## Basic concepts of binary

```
Binary is the carry system of every 2 carry, and 0 and 1 are the basic operators.
Modern electronic computer technology uses binary, because it only uses two digital symbols: 0 and 1, which is very simple and convenient and easy to be realized electronically. The information processed inside the computer is represented by binary numbers. Binary numbers represent any number with two numbers 0 and 1 and their combinations. The carry rule is "every 2 into 1". The number 1 represents different values in different bits. In the order from right to left, this value is doubled.
Note: 1 byte = 8 bits, sign bit 0 represents positive number and 1 represents negative number
```

- Binary – original code, inverse code, complement code

```
For signed:
1. The highest bit of binary is the sign bit: 0 represents a positive number and 1 represents a negative number
2. The original code, inverse code and complement of positive numbers are the same
3. Inverse code of negative number = the sign bit of its original code remains unchanged, and other bits are reversed
4. The complement of a negative number = its inverse code + 1
5. The inverse and complement of 0 are all 0
6. Java does not have unsigned numbers. In other words, all numbers in Java are signed
7. In computer operation, it is calculated in the way of complement.
```

## Bitwise Operators

```
There are four bit operations in Java, namely bitwise AND &, bitwise or |, bitwise XOR ^, and bitwise negation ~. Their operation rules are as follows:
Bitwise AND &: both bits are 1, and the result is 1
Bitwise or |: one of the two bits is 1, and the result is 1
Bitwise exclusive or ^: one of the two bits is 0, the other is 1, and the result is 1, that is, the same is 0 and the difference is 1
Reverse by bit: 0 - > 1, 1 - > 0
```

## Shift operation

```
There are three shift operators in Java:
>>, < < arithmetic shift right and arithmetic shift left, operation rules:
Arithmetic shift right: the low bit overflows, the sign bit remains unchanged, and the sign bit is used to fill the overflow high bit
Arithmetic shift left: the sign bit remains unchanged, and the low bit is supplemented by 0
>>>Logical shift to the right, the operation rule is: low overflow, high complement 0
```

## Code example

```
/**
*Binary shift operation and shift operation
*/
public class test1 {
public static void main(String[] args) {
System. Out. Println ("the shift operation result is as follows:");
//Arithmetic shift right and left
//Arithmetic shift right: the low bit overflows, the sign bit remains unchanged, and the sign bit is used to fill the overflow high bit
//Remember the relationship between original code, inverse code and complement code
//The original code, inverse code and complement of positive numbers are the same
//For the inverse code of the sign number, the sign bit remains unchanged, the rest is reversed, the complement is the inverse code, and 1 is added to the first bit
//The reverse is also true
int a = 1 >> 2;
int b = -1 >> 2;
//Arithmetic shift left: the sign bit remains unchanged, and the low bit is supplemented by 0
int c = 1 << 2;
int d = -1 << 2;
//Logical shift right
int e = 3 >>> 2;
System.out.println("1 >> 2:"+a);
System.out.println("-1 >> 2:"+b);
System.out.println("1 << 2:"+c);
System.out.println("-1 << 2:"+d);
System.out.println("3 >>> 2:"+e);
System.out.println("---------------------------");
System.out.println ("bit operation result is as follows");
//~Reverse: reverse by bit, 0 - > 1, 1 - > 0
System.out.println("~2:"+(~2));
//&And: both are 1, and the result is 1
System.out.println("2&3:"+(2&3));
//|Or: one of the two is 1 and the result is 1
System.out.println("2|3:"+(2|3));
//^XOR: two bits, one is 0, the other is 1, and the result is 1, that is, the same is 0 and the difference is 1
System.out.println("-3^3:"+(-3^3));
}
}
```

## Calculation process

```
Note: computer operations are performed in the form of complement. If the complement calculation result is negative, it is necessary to convert the complement to the original code
1>>2
Original code of 1
00000000 00000000 00000000 00000001
Because the original code, inverse code and complement of positive numbers are the same
So the complement of 1
00000000 00000000 00000000 00000001
Arithmetic shift right 2-bit result
00000000 00000000 00000000 00000000
So 1 > > 2 = 0
It is deduced that 1 > > n (positive integer of N > 1) is 0
-1>>2
-Original code of 1
10000000 00000000 00000000 00000001
-Inverse code of 1
11111111 11111111 11111111 11111110
-Complement of 1
11111111 11111111 11111111 11111111
Arithmetic shift right 2-bit result
11111111 11111111 11111111 11111111
Complement - > inverse: - 1
11111111 11111111 11111111 11111110
Inverse - > Original
10000000 00000000 00000000 00000001
So - 1 > > 2 = - 1
It is deduced that - 1 > > n (positive integer of N > 1) results in - 1
3>>>2
Original code of 3
00000000 00000000 00000000 00000011
Because the original code, inverse code and complement of positive numbers are the same
So the complement of 3
00000000 00000000 00000000 00000011
>>>The result of logical shift right by 2 bits is
00000000 00000000 00000000 00000000
So 3 > > > 2 = 0
~2
The original code and complement of 2 are the same
Complement of 2
00000000 00000000 00000000 00000010
Bitwise inversion~
11111111 11111111 11111111 11111101
Because the first bit is 1 and negative, it needs to be - 1, and the other bits are reversed to obtain the original code result
-1
11111111 11111111 11111111 11111100
Other bits are reversed
10000000 00000000 00000000 00000011
So the result is - 3
```

This work adoptsCC agreement, reprint must indicate the author and the link to this article