Programming apes may not be unfamiliar with binary system, which is widely used in computing technology. Binary data is a number represented by two numbers, 0 and 1. But many people will convert binary to integer, but how to use binary to represent negative numbers? Some people would say that adding a negative number before the binary is consistent. But the computer only knows 0 and 1, how to add an extra negative sign? So we need 0 and 1 to represent negative numbers. If we want to understand this, we need to first understand what binary source code is.

What is the original code?

Truform is a binary fixed-point representation of numbers in a computer. The original code representation adds a symbol bit (i.e. the highest bit is the symbol bit): the positive bit is 0, the negative bit is 1 (0 has two representations: +0 and-0), and the rest bits represent the size of the value.

Simple and intuitive; for example, we use 8-bit binary to represent a number, +11’s original code is 000011, -11’s original code is 10001011.

The original code can not directly participate in the operation, and may make mistakes. Mathematically, for example, 1+(-1) = 0, while in binary system 00000001 + 10000001 = 10000010, converted to decimal system – 2. Obviously something went wrong.

Binary Primitive Code, Complement Code and Counter Code

How to convert decimal system into binary system

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

`0000 0000 0000 0000 0000 0000 0000 0011`

Converting negative numbers to binary is divided into three steps:

**1. First, the negative number is converted to the corresponding source code.**

– The original code of 3 is (that is, +3 is converted into a binary string):

`0000 0000 0000 0000 0000 0000 0000 0011`

**2. Then each bit of the original code is inversely manipulated to get the inverse code.**

The reverse operation: 0 to 1, 1 to 0; the reverse result is:

`1111 1111 1111 1111 1111 1111 1111 1100`

**3. Get the complement from the inverse code + 1**

`1111 1111 1111 1111 1111 1111 1111 1101`

Now use the calculator that comes with Windows to verify that Win + R input calc, change the calculator to programmer, select double words (4 bytes, 32 bits)

Open the Calculator Scientific Computing Function of Windows

**Select decimal in the calculator, then enter – 3:**

Windows Computers Scientifically Calculate Decimal Input-3

Then click on the binary conversion to convert – 3 under decimal system into binary system:

Convert decimal-3 to binary

Binary to Decimal Negative Number Problem

Normally, there is no problem in converting binary to decimal. In integer types like Javascript / PHP, int / integer is usually limited to 32 bits. It also indicates that in these languages, integers have maximum values, and the 32-bit maximum integer limit is 2147483647, that is, binary:

`01111111111111111111111111111111`

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

**What is the decimal value of 32-bit binary 11111111111111111111111111001?**

`11111111111111111111111111111001`

As mentioned above, the length of the binary system is 32 bits, that is, the integer is a negative number.

`00000000000000000000000000000110`

Inverse code + 1, get:

`00000000000000000000000000000111`

Convert to decimal: 7

Because it’s a negative number, add a negative sign and convert it to -7.

**Interesting: What is the decimal value of 32-bit binary 1111111111111111111111101?**

This is an interesting one. Don’t mislead me into thinking that this is a negative number. In fact, this is an integer, because there are only 31 digits here. We need to add 0 to the front to make up 32 digits.

`01111111111111111111111111111001`

Conversion of decimal negative numbers to octal and hexadecimal numbers

Negative digits are converted to octal and hexadecimal digits. Only 3-bit synthesis or 4-bit synthesis is needed on the basis of complement (binary).

– The conversion of 3 to binary is:

`1111 1111 1111 1111 1111 1111 1111 1101`

In octal system, the binary of -3 is divided into one unit every 3 bits from right to left, and not enough three bits are complemented by 0:

`011 111 111 111 111 111 111 111 111 111 101`

Each element is calculated and the result is 377777775.

Hexadecimal system combines the binary of -3 from right to left into one unit, that is:

`1111 1111 1111 1111 1111 1111 1111 1101`

After calculation: FFFFFFFD

Convert decimal-3 to octal and hexadecimal