Arithmetic * */public class operator {public static void main of/* * bit (String[] args) {/* * right Shift >> signed positive 0 negative complement 1 * >>> unsigned positive complement 0 negative complement 1 */int a = 10 ;//converted to binary, backward 2-bit front 0, The last two Bits erase System.out.println (A>>2);//move one equivalent to be in 2system.out.println (Integer.tobinarystring (a>>2));// Unsigned shift System.out.println (A>>>2); System.out.println (integer.tobinarystring (a>>>2)) int b = -10 ;//front complement complement 1 Erase/* * reasoning process * a. binary 28 01010 * of binary * 10 with 10 first to get back &NBSP;28 10101---"+1 * -10 binary 28 10110 * b. Move 1128 1 01 This is the final negative. * Right move two-bit * c. Calculates the final result * negative negation +1 to get a corresponding positive number * 0028 010 +1------"get corresponding positive number 00 28 011 * Positive 3 corresponds to a negative number is -3 */system.out.println (B>>2);//Unsigned Right move specifically for negative numbers Negative numbers are also 0system.out.pr in front.Intln (B>>>2);/* * left move operation << back 0 (regardless of positive or negative) */system.out.println (a<<2);//left one, Multiply 2system.out.println (b<<2);}} & (Bit and), | (bit or), ^ (XOR) Package com.pb.polytest;public class operator2 {public static void main (String[] args) {int c1 = 5;int d1 = 4;/* * Convert two numbers to decimal and then one bit of the calculation * is 1 This bit is 1, otherwise 0 gets the binary converted to decimal */system.out.println (C1&NBSP;&&NBSP;D1) ;/* * convert two numbers to decimal and then one bit of calculation * is 0 This bit is 0, otherwise 1 gets the binary converted to decimal */system.out.println (C1 &NBSP;|&NBSP;D1);/* * convert two numbers to decimal then one bit of the calculation * the same as 0 different for the binary converted to decimal */ System.out.println (C1&NBSP;^&NBSP;D1);/* * if c =a ^ b ; * so a == c ^ b ; * b == a ^ c ; */}}
This article is from the "Java" blog, so be sure to keep this source http://5737386.blog.51cto.com/5727386/1661079
Bit operations in Java