Binary : The computer can only process binary data, using the algorithm provided by the programming language to support decimal; the computer's internal (Java) only has binary data, and the programming language provides API to convert the binary to decimal when displayed; the computer surface supports Decimal, but it is not supported.
There are two methods in Java:
Integer.toString () converts binary data to decimal output
Integer.parseInt () converts decimal string to binary data
int n=45;
System.out.println(n);//45,其实对n做了Integer.toString(n)。如下
System.out.println(Integer.toString(n));//"45"
System.out.println(Integer.toBinaryString(n));//101101
int i=0;
System.out.println(Integer.toBinaryString(i++));//0
System.out.println(Integer.toBinaryString(i++));//1
System.out.println(Integer.toBinaryString(i++));//10
System.out.println(Integer.toBinaryString(i++));//11
System.out.println(Integer.toBinaryString(i++));//100
Hexadecimal : Used to abbreviate binary (abbreviated binary), because the writing of binary is too long, every four bits of binary are abbreviated as a hexadecimal number, according to this rule, binary abbreviation can be used; When writing binary data, hex is used as an abbreviation.
//十六进制用于缩写二进制
int n=0xb5;//十六进制
System.out.println(Integer.toBinaryString(n));//二进制 :10110101
System.out.println(n);//十进制 :181
int m=0x5fdddb12;
System.out.println(Integer.toBinaryString(m));//1011111110111011101101100010010 高位自动省略
System.out.println(m);//1608375058
Complement : It is an algorithm that uses "positive numbers" to represent "negative numbers", saving hardware costs.
int n=-1;
System.out.println(Integer.toBinaryString(n));//11111111111111111111111111111111
System.out.println(Integer.toBinaryString(-2));//11111111111111111111111111111110
System.out.println(0);//0
for(int i=-10;i<10;i++){
System.out.print(Integer.toString(i)+" ");
System.out.println(Integer.toBinaryString(i));
}
int max=Integer.MAX_VALUE;
int min=Integer.MIN_VALUE;
System.out.println(Integer.toBinaryString(max));//1111111111111111111111111111111
System.out.println(Integer.toBinaryString(min));//10000000000000000000000000000000
System.out.println(min-max);//1
System.out.println(max+1);//-2147483648
System.out.println(min-1);//2147483647
int n=8;
int m=n+(max+1)*4;
System.out.println(m);//8
//补码的对称现象:-n=~n+1
n=8;
System.out.println(~n+1);//-8
System.out.println(Integer.toBinaryString(n));//1000
System.out.println(Integer.toBinaryString(~n));//11111111111111111111111111110111
System.out.println(Integer.toBinaryString(~n+1));//11111111111111111111111111111000
Binary operator :
1 ~ invert
2. >>> >> << shift operation
2.1) >>> logical right shift operation: move the number to the right, supplement the high bit with 0, and discard the low bit overflow
n = 01101101 00010001 11001001 10011011 m = n >>> 1
m = 001101101 00010001 11001001 1001101
2.2) << Logical left shift operation: move the number to the left, supplement the low bit with 0, and discard the high bit by
2.3) >>> and >>
: >>> Move to the right, the high bit is always filled with 0, and the negative number does not meet the mathematical division rule;:
>> The digit moves to the right, the high bit is 1 (negative number), then 1 is added, and the high bit is 0 (positive number), then 0 is filled, and the sign bit is maintained Unchanged, the result is in accordance with the law of mathematical division, automatically rounded to a small direction
2.4) Classic use of shift operation: cooperate with mask operation, split the data
3. & | AND OR operation
3.1) & AND operation logic multiplication: 1 & 1 = 1 0 & 1 = 0 1 & 0 = 0 0 & 0 = 0
Calculation rule: two numbers are aligned up and down, and the corresponding digits are ANDed
d = 01100011 00100110 00110111 11011110;
e = 00000000 00000000 00000000 11111111
f = d & e;
f = 00000000 00000000 00000000 11011110
Classic purpose: intercept the last eight bits of a data, called mask operation
3.2) | OR operation: merge the data
The rule is similar to addition 1 | 1 = 1 0 | 1 = 1 1 | 0 = 1 0 | 0 = 0
Calculation rule: two numbers are aligned up and down, corresponding to the number of bits OR operation
b1 = 00000000 00000000 00000000 10011101
b2 = 00000000 00000000 00000000 01101111
b3 = 00000000 00000000 00000000 11101111
b4 = 00000000 00000000 00000000 00110011
n = (b1 << 24) | (b2 << 16) | (b3 << 8) | b4
= 10011101 00000000 00000000 00000000
00000000 01101111 00000000 00000000
00000000 00000000 11101111 00000000
00000000 00000000 00000000 00110011
= 10011101 01101111 11101111 00110011
int n=0x6d11c99b;
// >>>逻辑右移位运算
int m1=n>>>1;
int k1=n>>>2;
System.out.println(Integer.toBinaryString(n));
System.out.println(Integer.toBinaryString(m1));
System.out.println(Integer.toBinaryString(k1));
// <<逻辑左移位运算
int m2=n<<1;
int k2=n<<2;
System.out.println(Integer.toBinaryString(n));
System.out.println(Integer.toBinaryString(m2));
System.out.println(Integer.toBinaryString(k2));
// >>>与>>
int a=-36;
int b=a>>1;
int c=a>>>1;
System.out.print(a+" ");
System.out.println(Integer.toBinaryString(a));
System.out.print(b+" ");//b=-18
System.out.println(Integer.toBinaryString(b));
System.out.print(c+" ");
System.out.println(Integer.toBinaryString(c));//??不符合数学规律
// &与运算
//将int d拆分为4个八位数 f1 f2 f3 f4
int d=0x632637de;
int e=0xff;
int f1=d&e;
int f2=(d>>>8)&e;
int f3=(d>>>16)&e;
int f4=(d>>>24)&e;
System.out.println(Integer.toBinaryString(d));//1100011 00100110 00110111 11011110
System.out.println(Integer.toBinaryString(e));//11111111
System.out.println(Integer.toBinaryString(f1));//11011110
System.out.println(Integer.toBinaryString(f2));//110111
System.out.println(Integer.toBinaryString(f3));//100110
System.out.println(Integer.toBinaryString(f4));//1100011