# Negative Number and Binary Conversion Method

Time：2019-9-10

1. Conversion of decimal negative numbers to binary numbers

Assuming that there is a number of int types with a value of 5, we know that it is represented in the computer as follows: (because in Java int is 4 bytes, so the high bit needs to be filled with 0, accounting for 32 bits)
00000000 00000000 00000000 00000101
Now I want to know how minus 5 is represented in a computer.
In computer, the negative number is expressed in the form of complement of the original code.
What is a complement? This has to start with the original code, the reverse code.

Original code: a positive number, converted into binary number according to the size of absolute value; a negative number converted into binary number according to the size of absolute value, and then the highest bit complement 1, known as the original code.
For example, 000 000 000 000 000 000 000 000 000 000 000 000 000 000 101 is the original code of 5; 10 000 000 000 000 000 000 000 000 000 000 101 is the original code of – 5.

Inverse Code: The inverse code of a positive number is the same as the original code, and the inverse code of a negative number is the inverse of the original code of that number except for the symbol bits.
Reverse operation refers to: original 1, get 0; original 0, get 1. (1 changes 0; 0 changes 1)
For example: the positive number of 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 101 or 000 000 000 000 000 000 000 000 101;
Negative number 100000000000000000000000000000000101, each negative bit (except the sign bit) gets 11111111111 11111 11111 11111010.
Said: 100000000000000000000000000000000000101 and 11111111111111111111111111111111111111010 are reciprocal codes.

Complement: The positive complement is the same as the original code. The negative complement is the reverse of the original code except for the symbol bit, and then adds 1 at the last bit.
For example, the reverse code of 100000000000000000000000000000101 is 11111111111 11111 11111 11111010.
Then, the complement is:
11111111 11111111 11111111 11111010 + 1 = 11111111 11111111 11111111 11111011
Therefore, -5 is expressed in computer as 11111111111 11111 11111 11111 11111 11011. Convert to hexadecimal: 0xFFFFFFFB.

For another example, let’s look at how integer-1 is represented in a computer.

Assuming that this is also an int type, then:
1. First fetch-1: 100000000000000000000000000000001
2. Inverse Code: 11111111111 11111 11111 11111 11111110
3. Complement: 11111111111 11111 11111 11111 11111111 11111 11111
It can be seen that the binary representation of -1 in a computer is the full 1. The hexadecimal system is: 0xFFFFFF

Main knowledge points:

(1) The inverse and complement codes of positive numbers are the same as the original codes.
(2) The inverse code of a negative number is the inverse of the original code of the number except the symbol bit.
(3) The complement of a negative number is to reverse the original code of the number except the symbol bit, and then add 1 to the last bit.

The following is the original text of the book:

The original code representation law stipulates that the signed number is represented by symbol bits and numerical values, the positive number by “0”, the negative number by “1”, and the numerical part by binary form.
(2) The inverse code representation law stipulates that the inverse code of a positive number is the same as the original code, and the inverse code of a negative number is the inverse of the original code of that number except for the symbol bit.
(3) The complement representation law stipulates that the positive complement is the same as the original code, and the negative complement is the reverse of the original code except the symbol bit, and then adds 1 at the last bit.
(4) The positive and negative zero complements are the same, [+0] complement =[-0] complement = 0000 0000B.

2. Binary to Decimal Negative Number with Symbol Bit 1

I believe you already know how to make a change, but here is a description.

(1) Subtract the sign bit by 1;

The result is the source code of the negative number.

(3) The source code is converted to the corresponding decimal system.

PS: For decimal negative number and binary conversion to this end, if there are errors or better ways, welcome to discuss.

## Java atomicinteger class using

A counter For ordinary variables, when multithreading operations are involved, classic thread safety problems will be encountered. Consider the following code: private static final int TEST_THREAD_COUNT = 100; private static int counter = 0; public static void main(String[] args) { final CountDownLatch latch = new CountDownLatch(TEST_THREAD_COUNT); Thread[] threads = new Thread[TEST_THREAD_COUNT]; for (int i = […]