The technique of negative number and binary commutation method _

Source: Internet
Author: User

1, Decimal negative turn binary system


Suppose there is a number of int type, and the value is 5, then we know that it is represented on the computer as: (because the int in Java is 4 bytes, so the high level needs to be 0 and 32 digits)
00000000 00000000 00000000 00000101
Now want to know,-5 how to express in the computer?
In the computer, negative numbers are expressed in the complement form of the original code.
What do you mean, "complement"? This has to start with the original code, the inverse code.

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

Inverse code: The inverse code of a positive number is the same as that of the original code, and the inverse of negative numbers is the original code of the numbers except for the symbol.
The reverse operation refers to: the original 1, 0; originally 0, 1. (1 change 0; 0 Change 1)
For example: Positive 00000000 00000000 00000000 00000101 of the inverse code or 00000000 00000000 00000000 00000101;
Negative numbers 10000000 00000000 00000000 00000101 per bit (except for symbol bit), 11111111 11111111, 11111111 11111010.
said: 10000000 00000000 00000000 00000101 and 11111111 11111111 11111111 11111010 Each other is the inverse code.

Complement: The complement of a positive number is the same as the original code, the complement of the negative numbers for the original code in addition to the symbol for everyone to take the counter, and then in the last one plus 1.
For example: 10000000 00000000 00000000 00000101 of the counter code is: 11111111 11111111 11111111 11111010.
So, the complement is:
11111111 11111111 11111111 11111010 + 1 = 11111111 11111111 11111111 11111011
So,-5 is expressed in the computer as: 11111111 11111111 11111111 11111011. Convert to 16:0xFFFFFFFB.

To take another example, let's look at how the integer 1 is represented in the computer.

Suppose this is also an int type, then:
1, first take-1 of the original code: 10000000 00000000 00000000 00000001
2, the Counter code: 11111111 11111111 11111111 11111110 (in addition to the symbol bit by the counter)
3, have the complement: 11111111 11111111 11111111 11111111
Visible,-1 in the computer with binary expression is all 1. 16 System: 0xFFFFFF

main knowledge Points:

The ① and complement of positive numbers are the same as the original code.
② the inverse code of negative numbers is the number of the original code in addition to the symbol for everyone to take the counter.
The complement of the ③ negative number is the inverse of the symbol for the original code, and then the last one adds 1.

The following is the original book:

① The original Code notation stipulates that: the symbol bit and the number of signed digits, positive signs of the symbol with "0", the number of negative sign with "1", the value of the part of the binary form of the expression.
The ②-code notation stipulates that the inverse code of a positive number is the same as that of the original code, and that the inverse code of negative numbers is the inverse of the original code, except for the symbol.
③ complement notation stipulates that: The complement of a positive number is the same as the original code, the complement of negative numbers for the original code in addition to the symbol, you take the opposite, and then in the last one plus 1.
④ positive zero and minus zero complement the same, [+0] complement =[-0] =0000 0000B.

2, the symbol bit is 1 binary decimal negative

I believe you already know how to turn the law, but here is the explanation.

① drop sign bit minus 1;

② to remove the symbol bit, bitwise counter, the result is a negative source;

③ source code into the corresponding decimal.

PS: For decimal negative and binary switch to this end, if there is a mistake or a better way to welcome the discussion.

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.