Binary representations of Java-printed integers (code and parsing)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.