Java: Binary (original code, inverse code, complement) and bitwise operations

Source: Internet
Author: User
Tags bitwise bitwise operators

One or two binary (original code, anti-code, complement)
    • The highest bit of binary is the sign bit ("0" represents a positive number, "1" represents a negative number);
    • There are no unsigned numbers in Java ;
    • The computer is computed with the complement of integers;
1. Original code: Converts an integer to a binary representation

In the case of type int, theint type is 4 bytes and a total of 32 bits .

For example, 2 of the original code is:00000000 00000000 00000000 00000010

-2 of the original code is:10000000 00000000 00000000 00000010

2. Anti-code

Inverse code for positive numbers: same as original code

Negative anti-code: The original code of the symbol bit unchanged, the other bits take the reverse

For example, the counter code of 2 is:11111111 11111111 11111111 11111101

3. Complement

The complement of positive numbers: Same as the original code

Complement of negative numbers: Anti-code +1

For example, the complement of 2 is:01111111 11111111 11111111 11111110

Second, bitwise operations

There are 4 bitwise operators in Java:

1.bitwise AND &:Two bits are 1 and the result is 1

For example, 2&3 = 2

2 The original code is: 00000000 00000000 00000000 00000010

3 The original code is: 00000000 00000000 00000000 00000011

2&3 The original code: 00000000 00000000 00000000 00000010 = 2

2.bitwise OR |:At least one bit is 1 and the result is 1

For example, 2|3 = 3

2 The original code is: 00000000 00000000 00000000 00000010

3 The original code is: 00000000 00000000 00000000 00000011

2|3 The original code: 00000000 00000000 00000000 00000011 = 3

3.Bitwise XOR or ^:Two bits one is 1, one is 0, and the result is 1

For example, 2|3 = 3

2 The original code is: 00000000 00000000 00000000 00000010

3 The original code is: 00000000 00000000 00000000 00000011

2^3 The original code: 00000000 00000000 00000000 00000001 = 1

4.Bitwise Counter ~:0 becomes 1, 1 becomes 0

For example, = 3

Reverse 2 of the original code: 11111111 11111111 11111111 11111101 (the complement of the reversed result, that is, the complement of 3.) We need to release the original code from the complement to get-3)

Convert to Counter code: 11111111 11111111 11111111 11111100 (complement minus 1)

Converted to original code: 10000000 00000000 00000000 00000011 =-3 (the symbol is the same, the other bits take the reverse)

Summarize
    1. Positive numbers of the original code, anti-code, and complement are the same;
    2. Negative number of the inverse code = The original code of the symbol bit unchanged, the other bits take the reverse;
    3. The complement of negative number = inverse code +1;
    4. 0 of the original code, anti-code, complement is 0;
    5. The computer is operated in the complement;
    6. The inverse is different from the inverse code;

2018-01-06 18:38:29

Java: Binary (original code, inverse code, complement) and bitwise operations

Related Article

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.