Third, let's talk about "negative binary numbers"

Source: Internet
Author: User

First, remember not to ask why:"In a computer, all data is expressed in binary."

Remember not to ask why:"In a computer, positive numbers are directly expressed and stored using the original code."

Remember not to ask why:"In a computer, a negative number is represented and stored by its complement code (that is, its reverse code + 1."


For a negative value (a signed value), you must first determine a fixed length (in other words, the number of digits of the binary number), and then set the leftmost highest bit to the symbol bit. The number of digits must be fixed to avoid confusion between the symbol bit and other digits.

As long as you know the digits of each value, you can find the symbol bit, which should be the leftmost one. If the sign bit is 0, the value is positive. If it is 1, the value is negative.


(1) original code notation rules: Use the decimal method to represent the symbol bits of the number with the leftmost highest bits (0 indicates a positive number and 1 indicates a negative number ), the remaining number of other digits indicates the absolute value of the number. (Key: UnderstandingSymbol bitAndOther digits)

BytesOriginal code: An integer, which is converted to a binary number based on its absolute value. The leftmost highest bit uses 0 or 1 to indicate positive and negative, which is called the original code.

Example 1: the original code of positive number 5 is 0000 0101.

The original code of negative-5 is: 1000 0101.


BytesReverse code: Reverse the bitwise of each digit except the symbol bit. The new binary number obtained is called the anticode. (Tip:The symbol bit remains unchanged.,Inverse of other bits)

(2) anticode Notation: the anticode of zero and positive numbers is the same as that of the original code. The anticode of a negative number is the inverse of each digit except the symbol bit.

Example 2: The positive number 5 is the same as the original code: 0000 0101.

The original code of negative-5 is: 1000 0101. The inverse code is obtained for each of the original codes of this number except the symbol bit. The inverse code is 1111 1010.


BytesComplement: The Anti-code plus 1 is called the complement code. (Tip:The symbol bit remains unchanged.,Take the other bits first and Add 1 to them.)

(3) Complement expression: the zero or positive complement code is the same as the original code. The negative complement code is used to reverse each digit of the original code except the symbol bit, and then add 1 to the last digit.

Example ③: the positive number 5 is the same as the original code: 0000 0101.

The original code of negative-5 is: 1000 0101. The original code of this number is reversed for each digit except the symbol bit. Then, 1 is added to the last digit to obtain the complement code: 1111 1011.


Therefore, in a computer, negative-5 is represented and stored by its complement code (that is, its reverse code + 1): 1111 1011.


(4) whether positive or negative, the highest bitwise of the reverse code or complement code is the same as the highest bitwise of the original code! (Please refer to the original code notation, as well as the anti-code notation and the Supplementary Code notation !)

Because the positive number or complement code are the same as the original code, the highest bit remains unchanged.

Because the negative number or complement Code is based on the original code, the highest bit remains unchanged, and each bit of other digits is reversed, and even 1 is added to the last bit.



All of the above teach us to convert the familiar decimal positive or negative numbers into the computer's familiar binary numbers:

(Decimal: the original code-> the symbol bit remains unchanged. First, the reverse code-> Add 1-> the complement code)

In turn, how can we convert a computer's familiar binary positive or negative number into a familiar decimal number?

(Complement-> first minus 1-> anticode-> the symbol bit remains unchanged, and then reverse-> original code: decimal)


First, you need to know the highest bit of the binary number: If the leftmost highest bit (that is, the sign bit) is 0, it is a positive number, and if it is 1, it is a negative number.

The positive number of the binary. to convert it to decimal, first write it as "weighted coefficient expansion", and then convert it to decimal by "adding weights by bit.

To convert a negative binary number to a decimal number, we must first understand that "in a computer, a negative number is its complement code (that is, its anticode + 1) to represent and store the "essence of this sentence, and then reverse push (The symbol bit remains unchanged.,The other bits are first subtracted from 1 and then reversed.):


For example, the original code of-10 in decimal format is: 1000 1010.Invert, Get the reverse code: 1111 0101, and then in the lastAdd 1, You have to make up the code: 1111 0110.

In reverse push, the binary 1111 0110 (can be understood as a known complement), firstMinus 1, The result is (reversed): 1111 0101; Each digit except the symbol bitInvertTo get (original code): 1000 1010; in the original code representation, the highest bit 1 represents a negative number, and the remaining digits represent the absolute value of the number, which is-10.


Let's look at the reverse push process:

1111 0110

First-1

---------

1111 0101

And then reverse:

---------

1000 1010 = in the original code notation, the highest bit 1 represents a negative number, and the remaining other digits represent the absolute value of the number. The value is-10.

(This is based on the complement principle to reverse the source code. It is equivalent to a clock with only and. It turns out to be at, and it can be switched)


Try to obtain the complement code for this binary number:

1111 0110

First reverse:

---------

1000 1001

Plus 1

---------

1000 1010 = It is understood in the original code notation, which is also in decimal format-10.

(The original code is obtained by adding 1 to the backend, And the complement code is obtained by adding 1 to the backend. This is shaking. It is equivalent to a clock with only 0 points and 6 points. It turns out to be 0 points and can be set clockwise to 6 points)


If the source code is known as the complement code, you can reverse the complement process (that is, the symbol bit remains unchanged, and the other bits are first reduced by 1 and then reversed) to obtain the original code, or the complement code is also obtained (that is, the symbol bit remains unchanged, the original code can be obtained after the other bits are obtained first and then added to the backend.

Actually, when you see a binary number, you can understand that you already know the complement code. You just need to use the above law to find the original code, and finally convert the original code to a decimal number. Isn't that enough?


Returns a binary number. If each operator is reversed (the highest sign bit is also reversed, so it is not an anticode), the obtained value is added to the original value, which is equal to-1. (Neither 0 nor 1)

Therefore, to obtain the inverse value of a binary number, it can be:-1-(the value of this binary number), that is, the opposite of the value of this binary number minus 1.

Third, let's talk about "negative binary numbers"

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.