This article mainly introduces the decimal negative conversion to binary, octal, hexadecimal knowledge sharing, the need for friends can refer to the next
Program apes may not be unfamiliar with binary systems, which are widely used in computing technology. Binary data is a number represented by 0 and 12 digits. But many people convert binary into integers, but how do you use binary notation for negative numbers? Some people will say that a negative number in front of the binary matches. And the computer can only know 0 and 1, how to add an additional negative sign? So we need to use 0 and a to represent negative numbers. If you want to understand this, we need to first understand what binary source code is.
What is the original code?
The source code (true form) is a binary fixed-point representation of a number in a computer. The original code notation adds a sign bit (that is, the highest bit to the sign bit) before the value: a positive number is 0, a negative number is 1 (0 has two representations: +0 and-0), and the remaining bits represent the size of the value.
Simple and intuitive; For example, we use 8-bit binary to represent a number, +11 of the original code is 00001011,-11 the original code is 10001011
The original code cannot participate directly in the operation and may be faulted. For example Mathematically, 1+ (-1) = 0, while in binary 00000001 + 10000001 = 10000010, translates to decimal-2. There was obviously a mistake.
Binary source code, complement, and inverse code
How decimal is converted into binary
How do we convert a decimal-3 to a binary representation? First we convert the absolute value of 3 to binary, assuming that it is of type int (32 bit), then the binary is represented as:
0000 0000 0000 0000 0000 0000 0000 0011
Negative numbers are converted into binary 3 steps:
1, first convert the negative number to the corresponding original code
3 The original code is (that is, +3 is converted to binary string):
0000 0000 0000 0000 0000 0000 0000 0011
2, and then the original code of every one to do anti-operation to get anti-code.
Reverse operation: 0 to 1, 1 to 0, and the inverse result is:
1111 1111 1111 1111 1111 1111 1111 1100
3, the anti-code +1 to get the complement
1111 1111 1111 1111 1111 1111 1111 1101
Now verify with the calculator that comes with Windows, win+r Input calc, change calculator to programmer, choose double Word (4 bytes, 32 bit)
Turn on the calculator for Windows comes with scientific computing features
Select Decimal in the calculator, then enter-3:
Windows comes with calculator scientific calculation decimal under input-3
Then click the binary conversion to convert the decimal 3 into binary:
Convert decimal-3 to Binary
Binary Turn decimal Negative number problem
Under normal circumstances, there is no problem in converting binary to decimal. In javascript/php-like integer types, general Int/integer have a bit limit, typically 32-bit lengths. It also indicates that integers in these languages have the largest value, while the 32-bit maximum integer limit is: 2147483647, which is binary:
01111111111111111111111111111111
Then it is easy to understand that the 32-bit binary, the first digit is 0, indicates that this is a positive number, and if it is 1, then it means that it is negative.
What is the 32-bit binary 1.,111,111,111,111,11e,+32 binary value?
11111111111111111111111111111001
As above, the binary length is 32 bits, that is, the integer is a negative number, the first negation, get the inverse code:
00000000000000000000000000000110
Anti-code +1, get:
00000000000000000000000000000111
Convert to decimal: 7
Because it is negative, add a minus sign, convert to 7
Fun: What is a 32-bit binary 1.,111,111,111,111,11e,+31 binary value?
This is a more interesting, do not mislead the above is a negative number, in fact, this is an integer, because there are only 31 bits, need to add 0 in front, 32-bit, to become:
01111111111111111111111111111001
Decimal negative turn octal, hex
Negative numbers are converted to octal, hexadecimal, just on the complement (binary) basis, 3-bit synthetic one calculation, or 4-bit synthesis of one calculation
-3 of the conversion into binary is:
1111 1111 1111 1111 1111 1111 1111 1101
Octal will be 3 binary from right to left every 3 bits for a unit, not enough three bits with 0 fill namely:
011 111 111 111 111 111 111 111 111 111 101
Calculating each cell, the result is: 37777777775
Hex merges the binary of 3 from right to left by merging each 4 bits into one unit, i.e.:
1111 1111 1111 1111 1111 1111 1111 1101
Calculated as: fffffffd
Convert decimal-3 to octal and hex
Decimal negative numbers converted to binary, octal, hexadecimal knowledge sharing