preface

Before we begin, let’s review the conversion between binary source, inverse, and complement and how computers deal with binary.

  • The highest bit in binary is the sign bit: 0 represents a positive number and 1 represents a negative number.
  • The original, inverse and complement codes of positive numbers are the same (three codes in one);
  • The inverse of a negative number = its original code symbol is unchanged, the other bits are reversed;
  • The complement of a negative number = its inverse +1 (the inverse of a negative number = its complement -1);
  • The inverse and complement of 0 are 0;
  • Java doesn’t have unsigned numbers;
  • At the bottom of the computer operation, are in the way of complement code to operate;
  • When we look at the result, we look at the source code.

An operator

  1. If both bits are 1, the result is 1; otherwise, the result is 0;
  2. Two bitwise or | : at least one is 1, 1, otherwise 0;
  3. If the two digits are different, it is 1. If the two digits are the same, it is 0.
  4. In reverse order: 0->1,1->0;
  5. Arithmetic right shift >> : low overflow, sign bit unchanged, with sign bit to complement the high overflow;
  6. Arithmetic left shift << : symbol bit unchanged, low complement 0;
  7. Logical right shift (unsigned right shift) >>> : low overflow, high fill 0. (Note no <<<)

Arithmetic calculations

public class BitOperator02 {
	public static void main(String[] args) {
		System.out.println(1>>2);/ / 0}}Copy the code
  • 1>> 2,1 arithmetic right shift: the low level overflows, the sign bit remains unchanged, and the high level overflows with the sign bit (note: computers always operate on complement)
  • The three positive codes are one, and the complement of 1 is 00000000 00000000 00000000 00000001
  • Source code of 1>>2:00000000 00000000 00000000 00000000– Overflow 01
  • So if I get 0, that’s the same thing as dividing 1, 2, 2 into 0
public class BitOperator02 {
	public static void main(String[] args) {
		System.out.println(-1>>2);/ / 1}}Copy the code
  • – Source code of 1:10000000 00000000 00000000 00000001
  • The inverse of -1 is 1111111111 11111111 11111111 11111110
  • The complement of -1:11111111 11111111 11111111 11111111
  • Use the sign bit that is 1 to complement the high level of overflow, -1>>2 complement: 111111111111 11111111 11111111– overflow 11
  • -1>>2 inverse code: 1111111111 11111111 11111111 11111110
  • – Source code of 1>>2:10000000 00000000 00000000 00000001
  • The result is 1
public class BitOperator02 {
	public static void main(String[] args) {
		System.out.println(1<<2);/ / 4}}Copy the code
  • 1<<2 1 arithmetic left shift: sign bit unchanged, low level complement 0
  • Source code of 1:00000000 00000000 00000000 00000001
  • 1<<2: 00000000 00000000 00000000 00000100
  • So it’s 4, which is equal to 122
public class BitOperator02 {
	public static void main(String[] args) {
		System.out.println(-1<<2);/ / - 4}}Copy the code
  • The complement of -1:11111111 11111111 11111111 11111111
  • Arithmetic left shift: the sign bit is unchanged, and 0 is added in the low position
  • -1<<2’s complement: 11111111 11111111 11111111 11111100
  • -1<<2 inverse code: 1111111111 11111111 11111111 11111011
  • – Source code of 1<<2:10000000 00000000 00000000 00000100
  • So that’s minus 4, which is the same thing as minus 1 times 2 times 2
public class BitOperator02 {
	public static void main(String[] args) {
		System.out.println(1>>>2);/ / 0}}Copy the code
  • Logical right shift (unsigned right shift) >>> : low overflow, high fill 0 (no <<< symbol)
  • The complement of 1:00000000 00000000 00000000 00000001
  • The complement of 1>>>2 is the original code: 00000000 00000000 00000000 00000000– overflow 01
  • The result is 0
public class BitOperator02 {
	public static void main(String[] args) {
		System.out.println(-1>>>2);/ / 1073741823}}Copy the code
  • The complement of -1:11111111 11111111 11111111 11111111
  • -1>>>2’s complement: 001111111111 1111111111 11111111 11111111
  • The results of 1073741823

conclusion

In particular, it should be noted that when logical right shift is unsigned right shift, 0 is used to supplement the high position, so it will always be a positive number. According to the principle of three codes in one, we can know that the changed complement is the original code.