Binary representation method for negative numbers

Source: Internet
Author: User

One: notation:
1. Notation for positive 5
Assuming that there is a number of type int with a value of 5, we know that it is represented in the computer as:
00000000 00000000 00000000 00000101
The 5 conversion to two is 101, but the number of int is 4 bytes (32 bits), so a heap of 0 is filled in front.

2, negative-5 notation
Now you want to know, how is 5 represented in the computer? In a computer, negative numbers are expressed in the form of the complement of the original code.

Second, the concept:
1, the original code: a positive number, according to the absolute value of the conversion into binary numbers, a negative number is converted to the absolute size of the binary, and then the highest bit 1, called the original code.
For example, 00000000 00000000 00000000 00000101 is the original code of 5.
10000000 00000000 00000000 00000101 is the original code of-5.

Note:
For example, a byte type, with 2^8 to represent unsigned integers, is 0-255, if there is a symbol, the highest bit represents the symbol, 0 is positive, and 1 is negative, then the normal understanding is 127 to +127. This is the original code, it is worth mentioning that the original code of weakness, there are 2 0, namely + 0 and 0 (10000000 and 00000000); There is, the addition or subtraction of the same number, compared to the idiot, the first to determine the absolute value of 2 numbers, and then add and subtract operations, the final operation of the result of the symbol is the same as the large symbol;

2, anti-code: Positive anti-code and the original code is the same, negative code for the number of the original code in addition to the sign bit out of each of you take the reverse [each bit to reverse (except the sign bit)].
Take the counter-operation means: The original is 1, get 0; 0, 1. (1 change 0; 0 Change 1)
For example: Positive 00000000 00000000 00000000 00000101 Anti-code or 00000000 00000000 00000000 00000101
Negative numbers 10000000 00000000 00000000 00000101 have an inverse code of 11111111 11111111 11111111 11111010.

The anti-code is mutual, so it can also be said: 10000000 00000000 00000000 00000101 and 11111111 11111111 11111111 11111010 are anti-code each other.

Note: There are still +0 and-0, not too long, the anti-code becomes the filter product, that is, the complement appeared later.

3, complement: positive complement and the original code is the same, minus the complement of the number of the original code in addition to the sign bit out of the counter, and then in the last one plus 1.
For example: 10000000 00000000 00000000 00000101 The complement is: 11111111 11111111 11111111 11111010.
Then, the complement is:
11111111 11111111 11111111 11111010 + 1 = 11111111 11111111 11111111 11111011

Note: 1, the method of seeking the original code from the complement is the same as the original code, can also be done by a complete inverse, first minus one, then take the reverse.
2, the complement but stipulates that 0 does not have the positive and negative points

So, 5 is expressed in the computer as: 11111111 11111111 11111111 11111011. Convert to 16 binary: 0xFFFFFFFB.


Thirdly, one more example
Let's see how the integer-1 is represented in the computer. Assuming this is also an int type, then:
1, first fetch-1 of the original code: 10000000 00000000 00000000 00000001
2, the anti-code: 11111111 11111111 11111111 11111110 (except for the symbol bit counter-bitwise)
3. Complement: 11111111 11111111 11111111 11111111

Visible, 1 in the computer with the binary expression is all 1. 16 Binary: 0xFFFFFF

Iv. main points of knowledge:

The inverse code and complement of positive numbers are the same as the original code.
The inverse of a negative number is the original code of the numbers in addition to the sign bit, you take the inverse.
The complement of negative numbers is the original code of the number except the sign bit, and then the last one plus 1.

Source: The advantage is that the simple disadvantage of conversion is that two 0 plus subtraction requires independent operation
Anti-code: A bit of a clear drawback is that two 0 plus subtraction also requires a separate operation
Complement: The advantage is that a 0 range of large subtraction can be converted to addition the disadvantage lies in understanding difficulties

Here is the original text of the book:

The original code notation specifies that the symbol number is represented by the sign bit and the numeric value, the sign bit of positive numbers is denoted by "0", the sign bit of negative numbers is denoted by "1", and the numerical part is represented by the binary form.
The anti-code notation stipulates that: The inverse code of positive numbers is the same as the original code, and the inverse code of negative numbers is the opposite of the number of the original code except the sign bit.
Complement notation: the complement of positive numbers is the same as the original code, the complement of negative numbers for the number of the original code in addition to the sign bit, and then the last one plus 1.
Positive zero and negative zero complement the same, [+0] complement =[-0] =0000 0000B

V. Special CASES-128
1000 0000, then, what is its original code? The method of seeking the original code from the complement is the same as the original code. Keep the sign bit other negation: 1111 1111, plus 1,11000 0000, more than 8 bits. Yes, the 8-digit source code cannot be represented here.

So, back to the original code, its original code is also 1000 0000 (out of automatic loss), 1000 0000 in the original code to indicate what? -0, but the complement stipulates that 0 has no positive or negative points.
Change your mind and see how the computer is calculated:
For negative numbers, take the absolute value first, then reverse, plus a
-128, 0111 1111, 0000, 1000 0000
It's clear now.
Therefore, the complement representation of the 8-bit signed integer value range
1000 0000 to 0000 0000, then to 0111 1111
That is, 128 to 0, then to 127.
Final-128 ~ +127

Always remember: the addition and subtraction of the program is always valid for binary binary. But not necessarily for the real world.

byte m =-128;
byte q = 1;
BYTE p = (byte) (M-Q); This step in fact the compiler will error, in fact, it is found out of bounds, we forcibly converted to byte can be seen results.
SYSTEM.OUT.PRINTLN (P); The result of P is: 127

Binary representation method for negative 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.