논리 부정 연산자
x | !x |
---|---|
true | false |
false | true |
public static void main(String[] args) {
int x = 0xAB, y = 0xF;
System.out.printf("x = %#X \t\t%s%n", x, toBinaryString(x));
System.out.printf("y = %#X \t\t%s%n", x, toBinaryString(y));
System.out.printf("%#X | %#X = %#X \t%s%n", x, y, x | y, toBinaryString(x | y));
System.out.printf("%#X & %#X = %#X \t%s%n", x, y, x & y, toBinaryString(x & y));
System.out.printf("%#X ^ %#X = %#X \t%s%n", x, y, x ^ y, toBinaryString(x ^ y));
System.out.printf("%#X ^ %#X ^ %#X = %#X %s%n", x, y, y, x ^ y ^ y, toBinaryString(x ^ y ^ y));
}
private static String toBinaryString(int x) { //진수 -> 2진수
String zero = "00000000000000000000000000000000";
String temp = zero + Integer.toBinaryString(x);
return temp.substring(temp.length()-32);
}
//결과
x = 0XAB 00000000000000000000000010101011
y = 0XAB 00000000000000000000000000001111
0XAB | 0XF = 0XAF 00000000000000000000000010101111
0XAB & 0XF = 0XB 00000000000000000000000000001011
0XAB ^ 0XF = 0XA4 00000000000000000000000010100100
0XAB ^ 0XF ^ 0XF = 0XAB 00000000000000000000000010101011
출처 : JAVA의 정석 - (남궁성지음)