java 按位操作符和移位操作符

 

 package Chapter3; import java.util.*; public class BitOperators { /** * @param args */ public static void main(String[] args) { int i1 = 0x2a; int i2 = 0x2b; System.out.println(Integer.toBinaryString(i1)); System.out.println(Integer.toBinaryString(i2)); System.out.println(Integer.toBinaryString(i1 & i2)); System.out.println(Integer.toBinaryString(i1 | i2)); System.out.println(Integer.toBinaryString(i1 ^ i2)); System.out.println(Integer.toBinaryString(~i1)); System.out.println(Integer.toBinaryString(~i2)); int ii = -1; System.out.println(Integer.toBinaryString(ii)); ii >>>= 10; System.out.println(Integer.toBinaryString(ii)); Random rand = new Random(47); int i = rand.nextInt(); int j = rand.nextInt(); System.out.println("-1: "+Integer.toBinaryString(-1)); System.out.println("1: "+Integer.toBinaryString(1)); int maxpos = Integer.MAX_VALUE; int maxneg = Integer.MIN_VALUE; System.out.println("maxpos: "+Integer.toBinaryString(maxpos)); System.out.println("maxneg: "+Integer.toBinaryString(maxneg)); System.out.println("i: "+Integer.toBinaryString(i)); System.out.println("~i: "+Integer.toBinaryString(~i)); System.out.println("-i: "+Integer.toBinaryString(-i)); System.out.println("j: "+Integer.toBinaryString(j)); System.out.println("i & j: "+Integer.toBinaryString(i & j)); System.out.println("i | j: "+Integer.toBinaryString(i | j)); System.out.println("i ^ j: "+Integer.toBinaryString(i ^ j)); System.out.println("i << 5: "+Integer.toBinaryString(i << 5)); System.out.println("i >> 5: "+Integer.toBinaryString(i >> 5)); System.out.println("(~i) >> 5: "+Integer.toBinaryString((~i) >> 5)); System.out.println("i >>> 5: "+Integer.toBinaryString(i >>> 5)); System.out.println("(-i) >>> 5: "+Integer.toBinaryString((-i) >>> 5)); int x = 0x2f; System.out.println("x: "+Integer.toBinaryString(x)); for(int k=0; k<6; k++){ x >>= 1; System.out.println("x"+k+": "+Integer.toBinaryString(x)); } int y = 0x2f; System.out.println("y: "+Integer.toBinaryString(y)); for(int k=0; k<6; k++){ y <<= 1; System.out.println("y"+k+": "+Integer.toBinaryString(y)); } for(int k=0; k<12; k++){ y >>= 1; System.out.println("y"+k+": "+Integer.toBinaryString(y)); } //用二进制形式表示char类型的值 char c = 'z'; int t = (int)c; System.out.println(t); System.out.println(Integer.toBinaryString(t)); } /*101010 101011 101010 101011 1 11111111111111111111111111010101 11111111111111111111111111010100 11111111111111111111111111111111 1111111111111111111111 -1: 11111111111111111111111111111111 1: 1 maxpos: 1111111111111111111111111111111 maxneg: 10000000000000000000000000000000 i: 10111010001001000100001010010101 ~i: 1000101110110111011110101101010 -i: 1000101110110111011110101101011 j: 1100110010110110000010100010110 i & j: 100010000000000000000000010100 i | j: 11111110011111110100011110010111 i ^ j: 11011100011111110100011110000011 i << 5: 1000100100010000101001010100000 i >> 5: 11111101110100010010001000010100 (~i) >> 5: 10001011101101110111101011 i >>> 5: 101110100010010001000010100 (-i) >>> 5: 10001011101101110111101011 x: 101111 x0: 10111 x1: 1011 x2: 101 x3: 10 x4: 1 x5: 0 y: 101111 y0: 1011110 y1: 10111100 y2: 101111000 y3: 1011110000 y4: 10111100000 y5: 101111000000 y0: 10111100000 y1: 1011110000 y2: 101111000 y3: 10111100 y4: 1011110 y5: 101111 y6: 10111 y7: 1011 y8: 101 y9: 10 y10: 1 y11: 0 122 1111010*/ }

你可能感兴趣的:(Java,平时学习积累)