Negative number and Binary Conversion Method

Source: Internet
Author: User
Tags binary to decimal

1. Convert negative decimal number to binary number

Suppose there is an int type number with a value of 5, so we know that it is represented as: (because int in java is 4 bytes, so it needs to be supplemented with 0 at a high level, 32 digits)
00000000 00000000 00000000 00000101
Now I want to know how-5 is represented in a computer?
In a computer, a negative number is expressed as a complement of the original code.
What is a supplemental code? This should start with the original code and the reverse code.

Original code: a positive number, which is converted to a binary number based on the absolute value. A negative number is converted to a binary number based on the absolute value, and the highest bit is supplemented by 1, which is called the original code.
For example, 00000000, 00000000, 00000000, 00000101, and 10000000, 00000000, 00000000, and 00000101 are-5.

Anticode: the anticode of a positive number is the same as that of the original code. The anticode of a negative number is the inverse of the original code of this number except the symbol bit.
The reverse operation indicates that the original value is 1, and the value is 0. The original value is 0, and the value is 1. (1 to 0; 0 to 1)
For example, if the positive number is 00000000, 00000000, 00000000, 00000101, 00000000, 00000000, or 00000000;
Negative number 10000000 00000000 00000000 each digit is reversed (except the symbol bit), 00000101 11111111 11111111 11111111.
Said: 10000000, 00000000, 00000000, 00000101, 11111111, 11111111, 11111111, 11111010, and are mutually inverse codes.

Complement: the positive value of the complement code is the same as the original code. The negative value of the complement code is used to reverse the original code of the number except the symbol bit, and then add 1 in the last bit.
For example, the 10000000 00000000 00000000 00000101 anti-code is: 11111111 11111111 11111111 11111010.
Then, the complement code is:
11111111 11111111 11111111 11111010 + 1 = 11111111 11111111 11111111
Therefore,-5 is expressed in the computer as 11111111 11111111 11111111 11111011. Convert to hexadecimal: 0 xFFFFFFFB.

Let's look at how integer-1 is represented in a computer.

Suppose this is also an int type, then:
1. First retrieve the original code of-1: 10000000 00000000 00000000
2. Get the reverse code: 11111111 11111111 11111111 11111110 (except for the bitwise inversion of the symbol)
3. Supplemental code: 11111111 11111111 11111111 11111111
It can be seen that the Binary Expression of-1 in a computer is full 1. Hexadecimal: 0 xFFFFFF

Key knowledge points:

① Positive numbers of backcodes and supplementary codes are the same as the original codes.
② The inverse code of a negative number is the inverse of the original code of this number except the symbol bit.
③ The complement of a negative number is the inverse of the original code of the number except the symbol bit. Then, add 1 to the last digit.

Below is the original article in the book:

① Original code Notation: The symbol bit and numerical value are used to represent the number of symbols. The positive sign bit is expressed as "0", the negative sign bit is expressed as "1", and the numerical part is expressed in binary form.
② Anti-code Notation: the positive number is the same as the original code, and the negative number is the inverse of the original code of this number except the symbol bit.
③ Complement expression: the positive complement is the same as the original code. The negative complement is used to reverse the original code of the number except the symbol bit, and then add 1 to the last digit.
④ The complement codes of positive and negative zeros are the same. [+ 0] fill = [-0] fill = 0000 0000B.

2. Binary to decimal negative number with a sign bit of 1

I believe you already know how to convert it, but I 'd like to explain it here.

① Remove the sign minus 1;

② Remove the symbol bit and return the reverse value by bit. The result is the source code of the negative number;

③ Convert the source code to the corresponding decimal.

PS: the conversion of decimal negative numbers and binary numbers ends here. If there is an error or a better method, you are welcome to discuss it.

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.