Java 移位符号分析

移位符号使运算更加快捷

有3种 <<
>>
>>>


下面来看例子,我们已经知道byte是8个 BIT, 一个int是4个 byte所以就是32个BIT
BIT 是 0 或 1 。

public class ShiftOperators {

    public static void main(String[] args) {
       
            int b = -123;
            getIntHexValue(b);
            b = (b << 2);
            getIntHexValue(b);
            b = (b>> 3);
            getIntHexValue(b);
            b = (b>>> 3);
            getIntHexValue(b);
            b = (b>> 35);
            getIntHexValue(b);
           
           
           
            byte c = ((byte)-123);
            byte g = (byte)(-123 & 0x7f);
            getByteHexValue(g);
           
            getByteHexValue(c);
           
            byte d = (byte) (c >>> 2);
            getByteHexValue(d);
   
            byte e = (byte) (c >> 2);
            getByteHexValue(e);
    }


public static void getIntHexValue(int val) {
    for (int i = 32; i >= 1; i--) {
        if ((i % 8) == 0) {
            System.out.print(" ");
        }
        int div = 1;
        for (int j = 1; j < i; j++) {
            div = div * 2;
        }
        System.out.print((val & div) / div);
    }
    System.out.println();
}

public static void getByteHexValue(byte val) {
    for (int i = 8; i >= 1; i--) {
        if ((i % 8) == 0) {
            System.out.print(" ");
        }
        byte div = 1;
        for (int j = 1; j < i; j++) {
            div = (byte) (div * 2);
        }
        System.out.print((val & div) / div);
    }
    System.out.println();
}

}


来看代码
未完待续。。。。

你可能感兴趣的:(java,C++,c,C#,J#)