Program apes may 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 will convert binary to integers, but how do you represent negative numbers in binary notation? Some people would say that adding a negative number in front of the binary fits. And the computer can only know 0 and 1, and how to add an extra negative sign? So we need to use 0 and a way to represent negative numbers. If you want to understand this, we need to first understand what binary source code is.

What's the original code?

The original code (true form) is a binary fixed-point representation of numbers in a computer. The original code notation adds a sign bit to the value (that is, the highest bit is the sign bit): A positive number is 0, the bit is 1 (0 has two representations: +0 and-0), and the remaining bits represent the size of the value.

For example, we use 8-bit binary to represent a number, +11 of the original code for 00001011,-11 is 10001011

The original code can not participate directly in the operation, may be an error. For example, in mathematics, 1+ (-1) = 0, whereas in binary 00000001 + 10000001 = 10000010, the conversion is decimal to 2. Obviously there was a mistake.

Binary source code, complement and inverse code

How to convert decimal into binary

How do we convert decimal-3 to binary notation? First, we convert the absolute value +3 of 3 to binary, assuming that it is of type int (32 bits), then the binary representation is:

0000 0000 0000 0000 0000 0000 0000 0011

The conversion of negative numbers into binary system is divided into 3 steps:

**1, first convert negative numbers to the corresponding original code**

-3 of the original code is (that is, +3 converted to binary string):

0000 0000 0000 0000 0000 0000 0000 0011

**2, then the original code for each of the reverse operation to get the code.**

Reverse operation: 0 becomes 1, 1 becomes 0, and the result of reverse is:

1111 1111 1111 1111 1111 1111 1111 1100

**3, the counter code +1 to get the complement**

1111 1111 1111 1111 1111 1111 1111 1101

Now use Windows's own calculator to verify that win+r input calc, change the calculator to programmer, select two words (4 bytes, 32 bits)

Turn on the Calculator scientific computing function with Windows self

**Select Decimal in the calculator, then enter-3:**

Windows own calculator scientific calculation decimal down input-3

Then click Binary conversion to convert decimal 3 to binary:

Convert decimal-3 to Binary

Binary Turn decimal negative problem

Normally, the conversion of binary to decimal is not a problem. But in the similar javascript/php and so on integer type, the general Int/integer all has the digit limit, generally is 32 bit length. Also indicates that integers in these languages have the maximum value, while the 32-bit maximum integer limit is: 2147483647, which is binary:

01111111111111111111111111111111

So it's easy to understand that 32-bit binary, when the first digit is 0, means that this is a positive number, and if it's 1, then that's negative.

**What is a 32-bit binary 1.,111,111,111,111,11e,+32 value?**

11111111111111111111111111111001

As above, the binary length is 32 digits, that is, the integer is a negative number, first reverse, get the inverse code:

00000000000000000000000000000110

Counter code +1, get:

00000000000000000000000000000111

Convert to decimal: 7

Because it's a negative number, add a minus sign and convert to 7.

**Fun: 32-bit binary 1.,111,111,111,111,11e,+31 What is the value of the binary?**

This is a more interesting, do not mislead to this is a negative number, in fact, this is an integer, because there are only 31 digits, need to add 0 in front, to make up 32 bits, into:

01111111111111111111111111111001

Decimal negative turn octal, hex

Negative numbers are converted into octal, hexadecimal, only on the basis of the complement (binary), 3-bit synthesis of a calculation, or 4-bit synthesis of a calculation

-The conversion of 3 to binary is:

1111 1111 1111 1111 1111 1111 1111 1101

The octal system 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

Calculate each unit, and the result is: 37777777775

Hexadecimal merges the binary of 3 from right to left every 4 bits into one unit, namely:

1111 1111 1111 1111 1111 1111 1111 1101

Calculated as: fffffffd

Converts decimal-3 to octal and hexadecimal