Computer binary summary, binary Summary
Binary
The Count rule goes one to two.
The cost of a binary computer is optimal.
Principle: everything in the computer is binary data!
Case:
Int I = 50; // I is in binary format in the computer! System. out. println (Integer. toBinaryString (I); System. out. println (I); // "50"
Binary
The Count rule goes one to two.
Case:
public static void main(String[] args) { for(int i=0;i<=50; i++){ System.out.println( Integer.toBinaryString(i)); }}
Hexadecimal
Hexadecimal: abbreviated as binary data. Each 4-digit binary can be abbreviated as a hexadecimal number.
Binary writing is very tedious.
01101001 00111111 01010101 01110101
Case:
Public static void main (String [] args) {int n = 0x693ba5e5; System. out. println (Integer. toBinaryString (n); // abbreviation of binary to hexadecimal, and verify the correctness of the abbreviation // 01110101 11111101 10101111 01011110 // 7 5 f d a f 5 e n = 0x75fdaf5e; System. out. println (Integer. toBinaryString (n); n = 50; System. out. println (Integer. toBinaryString (n ));}
Complement
Case:
public static void main(String[] args) { for(int i=-50; i<=50; i++){ System.out.println( Integer.toBinaryString(i)); }}
Case:
public static void main(String[] args) { int max = Integer.MAX_VALUE; System.out.println( Integer.toBinaryString(max)); int min = Integer.MIN_VALUE; System.out.println( Integer.toBinaryString(min)); max = 0x7fffffff; min = 0x80000000; int i = 0xffffffff; System.out.println(max); System.out.println(min); System.out.println(i);//-1}
Complementary symmetry of the complement code: public static void main (String [] args) {int n =-3; System. out. println (Integer. toBinaryString (n); System. out. println (Integer. toBinaryString (~ N); System. out. println (Integer. toBinaryString (~ N + 1); int m = ~ N + 1; System. out. println (m); // 3}
Int I = 0 xffffffff; System. out. println (I); output result of the above Code: A.2147483647 B .-2147483648 C.2147483648 D.-1 answer: Dint I = 0x80000000; System. out. println (I); output result of the above Code: A.2147483647 B .-2147483648 C.2147483648 D.-1 answer: B positive overflow number negative (wrong) positive overflow is a random number (wrong) int n = 5; system. out. println (~ N + 1); answer: (-5) int n = 5; System. out. println (~ N); answer: (-6) int n =-5; System. out. println (~ N); answer: (4) int n = 0 xfffffffe; // 11111111 11111111 11111111 11111110 System. out. println (~ N); // 00000000 00000000 00000000 00000001 answer: (1)
Binary Operators
- Reverse retrieval ~
- And &
- Or |
- Left shift operation <
- Right Shift of mathematics>
- Logical right shift >>>
Bit operation purpose: Text Encoding
Character: char type 16-bit Internet Data: 8-bit
If you use the Internet to transfer characters, you must split the characters into byte (8-bit) for transmission.
The character splitting scheme is called character encoding.
The simplest splitting scheme: UTF-16BE, split characters into two, both Chinese and English are 2-byte encoding. 1 byte is wasted in English and 65535 characters are supported.
A: 00000000 01000001 00000000 65B: 01000010 01001110 66 medium: 00101101 20013
Unicode: A non-repeating number of symbols. It has been encoded with more than 0.1 million characters.
Java char supports encoding: 65535 characters. We recommend that you use int to support extended Unicode.
UTF-8: Variable Length Encoding, English one byte, Chinese 3 byte, support 4 Byte encoding, support 1 million + characters.
Unicode of output characters
Public static void main (String [] args) {int n = '中'; System. out. println (Integer. toBinaryString (n ));}
And Operations & (logical multiplication)
Rules:
0 & 0 = 00 & 1 = 01 & 0 = 01 & 1 = 1
Two numbers, for its location, up and down count and
Case:
N = 00000000 00000000 01001110 00101101 m = 00000000 00000000 00000000 00111111 mask k = n & m 00000000 00000000 00000000 00101101
Code:
Int n = 0x4e2d; int m = 0x3f; // mask int k = n & m; System. out. println (Integer. toBinaryString (n); System. out. println (Integer. toBinaryString (m); System. out. println (Integer. toBinaryString (k ));
Or operation | (logical addition)
Rules:
0 | 0 = 00 | 1 = 11 | 0 = 11 | 1 = 1
Case:
n = 00000000 00000000 00000000 10000000m = 00000000 00000000 00000000 00101011k =n|m 00000000 00000000 00000000 10101011
Code:
int n = 0x80;int m = 0x2b;int k = n|m;System.out.println(Integer.toBinaryString(n));System.out.println(Integer.toBinaryString(m));System.out.println(Integer.toBinaryString(k)); n = 00000000 00000000 01001110 00101101
Case: encode character data (Unicode) as UTF-8 Encoding
Int c = '中'; int b3 = 0x80 | c & 0x3f; int b2 = 0x80 | (c >>> 6) & 0x3f; int b1 = 0xe0 | (c >>> 12) & 0xf;
Case: Decoding UTF-8 encoding into character data (Unicode)
int cc =((b1 & 0xf)<<12) | ((b2 & 0x3f)<<6) | (b3 & 0x3f);char ch = (char)cc;System.out.println(ch);
Mathematical significance of shift computing
Move decimal point calculation:
For example: 1234278. the result of moving the decimal point to the right is 12342780. results of 10 times different: 123427800. the difference is 100 times. If the decimal point does not move, the number moves to the left, for example, 1234278. the result of moving the decimal point to the right is 12342780. results of 10 times different: 123427800. difference 100 times
Promotion: When the binary system is used, the number is shifted to the left, and the number is doubled!
Case:
n = 00000000 00000000 00000000 00110010 50m = n<<1 0000000 00000000 00000000 001100100 100 k = n<<2 000000 00000000 00000000 0011001000 200
Code Verification
Int n = 50; int m = n <1; int k = n <2; 2 hexadecimal and 10 hexadecimal data of n m k
Optimization calculation n x 8 is () Answer: n <3
Difference >>>>>
Right Shift of mathematics>: The result satisfies the mathematical law. The entire division is rounded to the smallest direction, and the negative value is shifted. The result of adding 1 at the highest position is still a negative number.
n = 00000000 00000000 00000000 00110010 50m = n>>1 000000000 00000000 00000000 0011001 25k = n>>2 0000000000 00000000 00000000 001100 12n = 11111111 11111111 11111111 11001110 -50 m = n>>1 111111111 11111111 11111111 1100111 -25k = n>>2 1111111111 11111111 11111111 110011 -13
Logical right shift >>>: no matter whether it is positive or negative high, add 0!
n = 00000000 00000000 00000000 00110010 50m = n>>>1 000000000 00000000 00000000 0011001 25k = n>>>2 0000000000 00000000 00000000 001100 12n = 11111111 11111111 11111111 11001110 -50 m = n>>>1 011111111 11111111 11111111 1100111 k = n>>>2 0011111111 11111111 11111111 110011
Optimization Calculation Expression n = n + n/2 (n + = n/2) n> 0 answer: n + = n> 1
I am a beginner. If any updates are poor, I would like to thank you!
More highlights will be updated later!