Java bit operations

Source: Internet
Author: User
Public class bitoperate {public static void bitcalc () {byte A = 12, B = 10; byte c =-12, D =-10; /*** 1. bitwise AND & * (and) * Calculation Method: When the bitwise of both operands is 1, the result is 1. Otherwise, the result is 0. ** note: the symbol bit will also be involved in the operation * // 12 binary representation (1100), 10 binary mechanism (1010) system. out. println ("positive number and computation:" + (A & B); // 1100 & 1010 = 1000 = 8 // Note: A negative computer is represented by a supplementary code: //-12:11110100,-10: 11110110system. out. println ("negative number and calculation:" + (C & D); // (11110100) Fill = (10001100) original =-12 // system. out. println ("Positive and Negative Numbers and operations:" + (A & D ));/ /00001100 & 11110110 = 00000100 = 4 system. out. println ();/*** 2. bitwise OR | * (or) * Calculation Method: When the bitwise of the two operands has one side, the result is 1, otherwise, the value is 0 **/system. out. println ("positive number or operation:" + (A | B); // 1100 | 1010 = 1110 = 14 //-,-10: 11110110system. out. println ("negative number or operation:" + (c | D); // (11110110) Fill = (10001010) original =-10 // 1100,-10: 11110110system. out. println ("positive or negative number or operation:" + (A | D); // (11111110) Fill = (10000010) original =-2system. out. println ();/*** 3. Non-bitwise ~ * (Not) * operation method: the bitwise of the operand changes from 0 to 1, 1, and 0 * // 12: 20171100system. Out. println ("positive number non-operation:" + (~ A); // 11110011 = (10001101) original =-13 //-12: 11110100system. Out. println ("negative number non-calculation:" + (~ C); // 00001011 = 11system. out. println ();/*** 4. bitwise XOR or ^ * (XOR) * Calculation Method: if the value of 0 or 1 on each digit is the same, the result is 0, if it is different, 1 ** // 12: 00001100), 10: 00001010) system. out. println ("positive number exclusive or operation:" + (a ^ B); // 000000110 = 6 //-,-10: 11110110system. out. println ("negative exclusive or operation:" + (C ^ D); // 00000010 = 2 // 12:00001100,-10: 11110110system. out. println ("positive negative XOR operation:" + (a ^ D); // (11111010) Complement = (10000110) original =-6system. out. println ("--------- --------------------------------- ");/*** Shift operation ** concept: * shift operator is to translate a number based on binary. ** Category: * There are three types of rules based on the direction of translation and the number filling rule: <(left shift),> (shifted right with a symbol) and> (shifted right without symbol ). ** Operation method: * during the shift operation, the result after the byte, short, and char types are shifted to the int type. * when the values of byte, short, Char, and INT are shifted, it is specified that the actual number of moves is the number of moves and the remainder of 32, * That is, the result of 33 shifts is the same as that of 1 shift. * When moving a long value, the actual number of moves is the number of moves and the remainder of 64. * That is, the result of moving 66 times is the same as that of moving 2 times. * ** // ** 5. Left shift <** operation method: * shifts all digits to the right digit in binary format, and removes (discards) the digits from the upper vertex ), fill zero for low positions. ** Mathematical significance: * If a number does not overflow, for positive and negative numbers, moving one to the left is equivalent to multiplying the power of 1 to 2, and moving n to the left is equivalent to multiplying the power of N2. ** // Before: 0000 0000 0000 0000 0000 0000 0000 1100 0000 0000 // After: 0000 0000 0000 0000 0011 0000 = 48system. out. println ("shift positive to left:" + (A <2); // A * 2*2 = 48system. out. println ("negative shift left:" + (C <2); // C * 2*2 =-48 // number overflow: // 12 // before: 0000 0000 0000 0000 0000 0000 0000 1100 // <28 after: 1100 0000 0000 0000 0000 0000 0000 0000 // <29 after: 1000 0000 0000 0000 0000 0000 0000-0 completion Code indicates that the 31 Power of the minimum negative number 2 is-0000 83648system. out. println ("positive left overflow:" + (A <28); //-limit 3741824system. out. println ("positive left overflow:" + (A <29); //-2147483648 //-12 // before: 1111 1111 1111 1111 1111 1111 1111 0100 0100 // <28 after: 0000 0000 0000 0000 0000 0000 0000 1000 // <28 after: 0000 0000 0000 0000 0000 0000 running system. out. println ("negative shift left overflow:" + (C <28); // listen 3741824system. out. println ("negative shift left overflow:" + (C <29); //-2147483648system. out. println ();/*** 5, Shifted to the right with a symbol> ** operation method: * shifts all digits to the right in binary form, removes (discards) the low position, and supplements the sign bit with the high position, that is, the positive value is zero, and the negative value is 1. ** Mathematical significance: * If a number does not overflow, shifting one digit to the right is equivalent to dividing 2, and shifting N digits to the right is equivalent to dividing by the Npower of 2. ** // 10: 0000 0000 0000 0000 0000 0000 0000 1010 0000 //> 2 after: 0000 0000 0000 0000 0000 0000 0010 system. out. println ("positive number shifted to the right:" + (B> 2 )); // A/2*2 = 2 //-1111 1111 1111 1111 1111 1111 0110 111111 //> 2 after: 1111 1111 1111 1111 1111 1111 1101system. out. println ("negative number shifted to the right:" + (D> 2); //-3system. out. println ();/*** 5. Unsigned shift to the right >>> ** operation method: * shifts all digits to the right in binary format, low Position removal (discard), high vacancy fill zero. * For a positive number, it is the same as the shifts with the symbol to the right, but different for a negative number. ** Other structures are similar to>. ** // 10: 0000 0000 0000 0000 0000 0000 0000 1010 0000/> 2 after: 0000 0000 0000 0000 0000 0000 0010 system. out. println ("positive unsigned right shift:" + (B >>> 2 )); // The positive number is the same as> //-1111 1111 1111 1111 1111 1111 0110 001111 1111 //> 2 after: 1111 1111 1111 1111 1111 1101system. out. println ("negative unsigned right shift:" + (D> 2); // Add 0: 1073741821} public static void main (string [] ARGs) {bitoperate. bitcalc ();}}

Calculation Result:

Positive number and operation: 8 negative number and operation:-12 positive negative number and operation: 4 positive number or operation: 14 negative number or operation:-10 positive negative number or operation:-2 positive number non operation:-13 negative number non operation: 11 positive number exclusive or operation: 6 negative number exclusive or operation: 2 positive number exclusive or operation:-6 ---------------------------------------- positive number left shift: 48 negative number left shift overflow:-1073741824 positive number left shift overflow: -2147483648 negative shift left overflow: 1073741824 negative shift left overflow:-2147483648 positive shift to the right: 2 negative shift to the right:-3 positive unsigned shift to the right: 2 negative unsigned shift to the right: 1073741821
/*** Shift left of byte and short types ** will be automatically converted to int type shift right */public static void bitshift () {byte A = 64, B; int I; I = A <2; B = (byte) (a <2); system. out. println ("original value of A:" + a); system. out. println ("I (INT):" + I + "----- B (byte):" + B );}

Result:

Original value of a: 64i (int): 256----- b(byte): 0

A summary of the shift operation:

1. Left shift operation:

1> move one bit left, and the higher bit is removed (and discarded). Fill the right with 0. This means that when the number of left-shifted operations is of the int type, every time one bit is moved, its 31st bits will be removed and discarded; when the number of left-shifted operations is of the long type, every time one bit is moved, its 63rd bits will be removed and discarded.

2> the Left shift can double the original operand. programmers often use this method for Fast Multiplication of 2. But be careful, if you move 1 to a higher-order bit (31 or 63 digits), the value will change to a negative value.

3> during the shift operation on values of the byte and short types, Java will automatically extend these types to the int type, and the value after the shift is also the int type; if the left shift does not exceed 31 bits, the corresponding values will not be discarded. However, if you perform a shift operation on a negative byte or short value, after it is extended to the int type, its symbols are also extended, the result is filled with 1. Therefore, in order to get the correct result, you must discard the high level of the result. The simplest way to do this is to convert the result of the shift operation to the byte type.

2. For the right shift operation: each right shift is equivalent to dividing the value by 2 and discarding the remainder. You can use this feature to quickly divide an integer by two. Of course, you must ensure that you do not remove any of the original digits of the number.

3. The difference between the unsigned right shift (>>>) and the Right Shift: each right shift,> the operator always automatically uses its previous highest bit content to fill in its highest bit. In this way, the original value symbol is retained. The unsigned movement is always at the high (leftmost) to add 0.

4. unlike C and C ++, Java does not contain unsigned integers and specifies the memory bytes occupied by integer and floating point data, this ensures security, robustness, and platform independence.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.