develog

[Java] bit & shift 연산 본문

Dev/Java

[Java] bit & shift 연산

냐옴 2014. 7. 26. 21:53

& : 두 비트가 같으면 1, 다르면 0

| : 두 비트중 하나라도 1이면 1, 아니면 0

^ : 두 비트가 다르면 1, 같으면 0

~ : 1 이면 0, 0 이면 1

<< : 왼쪽으로 이동후 오른쪽 비트를 0 으로 채움

>> : 오른쪽으로 이동후  왼쪽 비트를 왼쪽 첫 비트와 같은 비트로 채움 (0이면 0으로 채우고 1이면 1로 채움)

>>> : 오른쪽으로 이동후 왼쪽 비트를 0 으로 채움



source

public class TestMain {

public static void main(String[] args) throws Exception {

System.out.println(" 25        : " + lpad8(Integer.toBinaryString(0xff & 25)));

System.out.println(" 255       : " + lpad8(Integer.toBinaryString(0xff & 255)));

System.out.println(" 25 & 255  : " + lpad8(Integer.toBinaryString(0xff & (25 & 255))));

System.out.println();

System.out.println(" 25        : " + lpad8(Integer.toBinaryString(0xff & 25)));

System.out.println(" 0         : " + lpad8(Integer.toBinaryString(0xff & 0)));

System.out.println(" 25 | 0    : " + lpad8(Integer.toBinaryString(0xff & (25 | 0))));

System.out.println();

System.out.println(" 25        : " + lpad8(Integer.toBinaryString(0xff & 25)));

System.out.println(" 255       : " + lpad8(Integer.toBinaryString(0xff & 255)));

System.out.println(" 25 | 255  : " + lpad8(Integer.toBinaryString(0xff & (25 | 255))));

System.out.println();

System.out.println(" 25        : " + lpad8(Integer.toBinaryString(0xff & 25)));

System.out.println(" 255       : " + lpad8(Integer.toBinaryString(0xff & 255)));

System.out.println(" 25 ^ 255  : " + lpad8(Integer.toBinaryString(0xff & (25 ^ 255))));

System.out.println();

System.out.println(" 25        : " + lpad8(Integer.toBinaryString(0xff & 25)));

System.out.println(" ~25       : " + lpad8(Integer.toBinaryString(0xff & ~25)));

System.out.println();

System.out.println(" 255       : " + lpad8(Integer.toBinaryString(0xff & 255)));

System.out.println(" ~255      : " + lpad8(Integer.toBinaryString(0xff & ~255)));

System.out.println();

System.out.println(" 256       : " + lpad32(Integer.toBinaryString(256)));

System.out.println(" 256 <<  2 : " + lpad32(Integer.toBinaryString(256 << 2)));

System.out.println(" 256 >>  2 : " + lpad32(Integer.toBinaryString(256 >> 2)));

System.out.println(" 256 >>> 2 : " + lpad32(Integer.toBinaryString(256 >>> 2)));

System.out.println();

System.out.println("-256       : " + lpad32(Integer.toBinaryString(-256)));

System.out.println("-256 <<  2 : " + lpad32(Integer.toBinaryString(-256 << 2)));

System.out.println("-256 >>  2 : " + lpad32(Integer.toBinaryString(-256 >> 2)));

System.out.println("-256 >>> 2 : " + lpad32(Integer.toBinaryString(-256 >>> 2)));

}

public static String lpad8(Object obj) {

String str = String.valueOf(obj);

while (str.length() < 8) {

str = "0" + str;

}

return ">" + str + "<";

}

public static String lpad32(Object obj) {

String str = String.valueOf(obj);

while (str.length() < 32) {

str = "0" + str;

}

return ">" + str + "<";

}

}


console

 25        : >00011001<

 255       : >11111111<

 25 & 255  : >00011001<


 25        : >00011001<

 0         : >00000000<

 25 | 0    : >00011001<


 25        : >00011001<

 255       : >11111111<

 25 | 255  : >11111111<


 25        : >00011001<

 255       : >11111111<

 25 ^ 255  : >11100110<


 25        : >00011001<

 ~25       : >11100110<


 255       : >11111111<

 ~255      : >00000000<


 256       : >00000000000000000000000100000000<

 256 <<  2 : >00000000000000000000010000000000<

 256 >>  2 : >00000000000000000000000001000000<

 256 >>> 2 : >00000000000000000000000001000000<


-256       : >11111111111111111111111100000000<

-256 <<  2 : >11111111111111111111110000000000<

-256 >>  2 : >11111111111111111111111111000000<

-256 >>> 2 : >00111111111111111111111111000000<


'Dev > Java' 카테고리의 다른 글

[Spring] dbcp 설정  (0) 2014.08.08
[Java] byte <-> int  (0) 2014.07.27
byte, int, 한글  (0) 2014.07.26
Quartz Clustering  (0) 2014.06.24
The Tomcat JDBC Connection Pool  (0) 2014.06.12
Comments