Binary representations of Java-printed integers (code and parsing)
int a=-99;for (int i=0;i<32;i++) { int t= (A & 0x80000000>>>i) >>> (31-i); System.out.print (t);}
In order to understand the problem of this shift output, you need to understand the following:
* 0x80000000 is the hexadecimal representation of the number, which is converted to binary representation as 10000000000000000000000000000000
* Priority of operation, shift operation higher than logical operation,>>> higher than &
* Bit logic and operation 1&1 = 1, 0&1 = 0
* >>> unsigned Right shift, remove partial discard, left bit 0;
The execution order of the statement block for the FOR loop:
1 0x80000000 unsigned right shift I bit;
The results of 2 A and 1 do bitwise with;
3 2 result unsigned right shift 31-i bit
4 Results of output 3
-99 of the number of machines (complement) indicated
11111111111111111111111110011101
The Loop process demonstrates:
i = 0
1 10000000000000000000000000000000 >>> 0 = 10000000000000000000000000000000
2 11111111111111111111111110011101 & 10000000000000000000000000000000 = 10000000000000000000000000000000
3 10000000000000000000000000000000 >>> (31-0) = 00000000000000000000000000000001
4 output 00000000000000000000000000000001, screen display "1"
i = 1
1 10000000000000000000000000000000 >>> 1 = 01000000000000000000000000000000
2 11111111111111111111111110011101 & 01000000000000000000000000000000 = 01000000000000000000000000000000
3 10000000000000000000000000000000 >>> (31-1) = 00000000000000000000000000000001
4 output 00000000000000000000000000000001, screen display "1"
......
i = 30
1 10000000000000000000000000000000 >>> 30 = 00000000000000000000000000000010
2 11111111111111111111111110011101 & 00000000000000000000000000000010 = 00000000000000000000000000000000
3 00000000000000000000000000000000 >>> (31-30) = 00000000000000000000000000000000
4 output 00000000000000000000000000000000, screen display "0"
i = 31
1 10000000000000000000000000000000 >>> 31 = 00000000000000000000000000000001
2 11111111111111111111111110011101 & 00000000000000000000000000000001 = 00000000000000000000000000000001
3 00000000000000000000000000000001 >>> (31-31) = 00000000000000000000000000000001
4 output 00000000000000000000000000000001, screen display "1"
According to the cycle process can be seen
(0x80000000 >>> i) The role is mask, each cycle with a do logic and operation, take the first bit of A;
The effect of the previous result >>> 31-i is to shift the first of the removed a to the lowest.
Binary representations of Java-printed integers (code and parsing)