Before Java bit operations, first need to understand the computer code, complement, and inverse knowledge.

Source code representation is a simple representation of the number of machines.

Its sign bit with 0 represents the positive sign, with: represents the negative sign, the value is generally expressed in binary form.

The inverse of the machine number can be obtained from the original code. If the machine number is positive, the inverse code of the machine number is the same as the original code. If the number of machines is negative, the inverse of the number of machines is obtained by taking bits of its source code (except sign bits).

The complement of the machine number can be obtained from the source code. If the number of machines is positive, the complement of the number of machines is the same as the original code.

If the number of machines is negative, the complement of the number of machines is obtained by inverting the bits of its source code (except the sign bits) and incrementing the bits by one.

Because the computer is used to store the value of the complement code to store, so the negative number of the calculation of the first to calculate its complement value

The following Java instance calculation some bits of the operators First is operator into & | ~ ^ > > < < > > > meaning respectively with ‘&’, or ‘|’, not ‘~’, an exclusive or ‘^’, moves to the right ‘> >’ left ‘< <‘, Where the bit and operation is 1 only when the two are 1, otherwise 0 bit or operation is 1 when one of the two is 1, otherwise 0 bit is not operation, 0 is converted into 1,1 is converted into 0, first the high level sign bit from 0 to 1, xor operation is 0 when both are the same, otherwise it is 1

int a=129; //10000001 int b=128; //10000000 system.out. println(" bit and operation "+(a&b)); / / person with operation when both to 1 to 1, otherwise 0 to 1000, 0000 = 128 System. Out. The println (" position or operation "+ (a | b)); Println (" bit non-operation "+(~b)); // bit non-operation, 0 to 1,1 to 0, 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 0111 1111 // the result is 1000 0000 0000 0000 0000 1000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0001 00000001=1 00000001=1 00000001=1 00000001=1




















int c=128; //1000 0000 int d=4; System.out.println(" move symbol right ="+(c>>d)); // Moving four to the right is equal to dividing by two to the fourth power, which is equal to 128/16=8, 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 System.out.println(" left shift ="+(c<<d)); // Moving four to the left is the same thing as multiplying by two to the fourth, 128*16=2048 // 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000=2 ^ 11 =2048 System.out.println(" unsigned right shift ="+(c>>>d)); // Moving four to the right is equal to dividing by two to the fourth power, which is equal to 128/16=8, 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000

The result is

Sign shift right =8 Sign shift left =2048 No sign shift right =8


Below, the negative numbers are calculated by right shift left shift and unsigned right shift

int m=-128; int n=4; System.out.println(" symbol shift right ="+(m>>n)); // 1000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 -128 // 1111 1111 1111 1111 1111 1111 0111 1111-128 // 1111 1111 1111 1111 1111 1111 1111 1111 1111 1000 0000-128 // 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1000 1000-128 // 1111 1111 1111 1111 1111 1111 // 1000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 System.out.println(" left shift ="+(m<<n)); // 1111 1111 1111 1111 1111 1111 1111 1000 0000-128 // 1111 1111 1111 1111 1111 1111 1000 0000 0000 0000, // 1000 0000 0000 0000 0000 0000 0000 0111 1111 1111 // 1000 0000 0000 0000 0000 0000 0000 System.out.println(" unsigned right shift ="+(m>>>n)); //1111 1111 1111 1111 1111 1111 1000 0000-128 //0000 1111 1111 1111 1111 1111 1111 1111 1111 1111 1000

Sign shift to the right =-8 Sign shift to the left =-2048 No sign shift to the right =268435448

Again, the source, inverse and complement of positive numbers are all the same. So when you figure out the answer, you just convert it to a positive value


Int (0b111111); int (0b111111); int (0b111111)

If the promoted type of the left-hand operand is int, then only the five lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator& (§15.22.1) with the mask value 0x1f (0b11111). The shift distance actually used is therefore always in the range 0 to 31, inclusive.

If the promoted type of the left-hand operand is long, then only the six lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & (§15.22.1) with the mask value 0x3f (0b111111). The shift distance actually used is therefore always in the range 0 to 63, inclusive.




int k=2048;

int l=-30;

System.out.println(k>>l);

>>(-30&0b11111) //-30 is 1000 0000 0000 0000 0000 0000 0001 1110 //-30 is 1111 1111 1111 1111 1111 1111/0001/1110-30 complement is 1111 1111 1111 1111 1111 1111 1110 0010 / / 0000 0000 0000 0000 0000 0000 0001 1111 int take five after, 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 10

Older Hello World