Analysis of negative-to-binary binary in Java

Source: Internet
Author: User

Recently looking at the collection of source code, found Arraydeque inside used a lot of & operations, which involves the binary system. Suddenly found themselves a bit fuzzy on the binary of negative numbers, and some support for this.

First of all we have a knowledge of the original code, anti-code and complement:

1, the so-called original code is the binary fixed-point notation, that is, the highest bit is the sign bit, "0" is positive, "1" is negative, the remaining bits represent the size of the value. 2, anti-code notation: the inverse code of the positive number is the same as its original code; the inverse of a negative number is a bitwise negation of its original code, except for the sign bit. The original code 10010 = anti-code 11101 (10010,1 is a character number, it is negative) (11101) binary = 130 3, the complement notation stipulates: The complement of positive numbers is the same as the original code, the complement of negative numbers in its inverse code and 1.

For example, let's look at how integer-1 is represented in a computer.
Assuming this is also an int type, then:
1, first take 1 of the original code: 00000000 00000000 00000000 00000001
2, the reverse code: 11111111 11111111 11111111 11111110
3. Complement: 11111111 11111111 11111111 11111111

Hexadecimal representation: 0xFFFFFFFF

So in Java, the result of integer.tobinarystring (-5) is 11111111111111111111111111111011. The integer is a 32-bit (bit).

Binary rules for negative numbers:

1, take the absolute value of the original code;

2. Calculate the inverse code of the original code;

3, add one to the anti-code, get the complement.


The source of the fall: http://blog.csdn.net/cl05300629

Analysis of negative-to-binary binary in Java

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.