[Java] package test;/** Author: Zhong Zhigang * content: binary (original code, reverse code and complement code), bitwise operator, shift operation * Time: www.2cto.com * 1.> shifts the arithmetic value to the right, the symbol bit remains unchanged, the Low Bit overflows, And the symbol bit is used to fill in the missing high position * <The arithmetic value shifts left, the symbol bit remains unchanged, and the low bit fills in zero *> logic shifts right, the low Overflow, complement 0*2 at a high level, bitwise operation :~ 1. bitwise inversion, (0 --> 1, 1 --> 0) *-3 ^ 3 returns an exclusive bitwise OR. If one is 0 and the other is 1, it is 1; * 1 & 3, bitwise AND, two digits are all 1. the result is 1; * 1 | 3, bitwise OR. If either of the two is 1, it is 1; * 3, binary anti-complement rules: For signed; * 1. The highest bit is the symbol and symbol bit, 0 is positive, and 1 is negative. * 2. The original code of positive number or 0. The reverse code is the same as the complement code. * 3. Negative anticode = its original code symbol bit remains unchanged. Others are reversed (0 --> 1, 1 --> 0) * 4. Negative complement = anticode + 1; * 5. There is no unsigned number in java, that is, the numbers in java are all signed numbers * 6, and all computation on a computer is done by Code complement; * ***/public class binary {/*** @ param args */public static void main (String [] args) {binary er = new binary ();} public binary () {// shift byte a = 1> 2; // low Overflow, use the symbol bit to fill in the missing high position // 1 --> 00000001-complement code-> 00000001-shift-> 00000000.01 --> 00000000 = 0 byte aa = 8> 2; // low Overflow, use the symbol bit to fill in the missing high position // 8 --> 00001000 -Complement-> 00001000-shift-> 00000010.00 --> 00000010 = 2 = 8 square bytes B =-1> 2; //-1 --> 10000001-complement-> 11111111-shift-> 11111111.11-complement-> 10000001 =-1 byte c = 1 <2; // high overflow, 0 complement low position // 1 --> 00000001-complement-> 00000001-shift-> 00000100 --> 4 = 2 square byte d =-1 <2; //-1 --> 10000001-complement-> 11111111-shift-> 11111100-complement-> 10000100 =-4 byte e = 3 >>> 2; // low Overflow, high Position 0 // 3 --> 00000011-logical shift-> random placement, 11 --> 00000000 = 0 int Ee =-3> 2; // low Overflow, 0 in the upper position // 3 --> 0011-24 0-1111 --> 111-24 1-11101-logical shift->-24 1-11,01 -->-24 1-System. out. println ("a (1> 2) =" + a); System. out. println ("B (-1> 2) =" + B); System. out. println ("c (1 <2) =" + c); System. out. println ("d (-1 <2) =" + d); System. out. println ("e (3 >>> 2) =" + e); System. out. println ("ee (-3 >>> 2) =" + (-3 >>> 2); // binary computation process byte f = 1; byte g = 2; /* f = 00 000001; // byte is a byte, eight-bit * g = 00000010 * f-g --> f + (-g) *-g = 10000010 * first, the inverse of g is: 11111101 * re-obtain the complement code: the anti-code of 11111110 * f is itself 00000001 * f-g = 11111111 * and then returns the reverse code: 10000000 --> 10000001 * That Is, f-g =-1 * // bit operation: byte h = ~ 2; // 2 --> 00000010--> 11111101->-> 10000010-+ 1-> 10000011 --> 3 byte I = 2 & 3; // If two values are 1, the value is 1. Otherwise, the value 0 // 2 --> 00000010, 3 --> 00000011; 2 & 3 = 00000010 --> 2 byte j = 2 | 3; // if one is 1, it is 1; otherwise, it is 0 // 2 --> 00000010, 3 --> 00000011; 2 | 3 = 00000011 --> 3 byte k = ~ -5; //-5 --> 10000101-complement-> 11111011-reverse-> 00000100-complement-> 00000100 --> 4 byte l = 13 & 7; byte m = 5 | 4; byte n =-3 ^ 3; // if the difference is 1, 0 //-3 --> 10000011-complement-> 11111101 --> 00000011,-3 ^ 3 --> 11111110-complement-> 10000010 =-2 System. out. println ("h (~ 2) = "+ h); System. out. println ("I (2 & 3) =" + I); System. out. println ("j (2 | 3) =" + j); System. out. println ("k (~ -5) = "+ k); System. out. println ("l (13 & 7) =" + l); System. out. println ("m (5 | 4) =" + m); System. out. println ("n (-3 ^ 3) =" + n );}}