| 입력(A) | 입력(B) | 출력(F) |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
ex)
1001(9)
1011(11)
-> 1001(9)
코드
public class Bit {
public static void main(String[] args) {
int a = 9;
int b = 11;
System.out.println("2진수 a: " + Integer.toBinaryString(a));
System.out.println("2진수 b: " + Integer.toBinaryString(b));
System.out.println("2진수 and 연산: " + Integer.toBinaryString(a & b));
System.out.println("10진수 and 연산: " + (a & b));
}
}
이때 Integer.toBinaryString(a)는 10진수 -> 2진수(문자열)

| 입력(A) | 입력(B) | 출력(F) |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
ex)
1001(9)
1011(11)
-> 1011(11)
코드
public class Bit {
public static void main(String[] args) {
int a = 9;
int b = 11;
System.out.println("2진수 a: " + Integer.toBinaryString(a));
System.out.println("2진수 b: " + Integer.toBinaryString(b));
System.out.println("2진수 or 연산: " + Integer.toBinaryString(a | b));
System.out.println("10진수 or 연산: " + (a | b));
}
}

| 입력(A) | 입력(B) | 출력(F) |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
ex)
1001(9)
1011(11)
-> 0010(2)
코드
public class Bit {
public static void main(String[] args) {
int a = 9;
int b = 11;
System.out.println("2진수 a: " + Integer.toBinaryString(a));
System.out.println("2진수 b: " + Integer.toBinaryString(b));
System.out.println("2진수 xor 연산: " + Integer.toBinaryString(a ^ b));
System.out.println("10진수 xor 연산: " + (a ^ b));
}
}

| 입력(A) | 출력(F) |
|---|---|
| 0 | 1 |
| 1 | 0 |
ex)
1001(9)
-> 0110(6)
코드
public class Bit {
public static void main(String[] args) {
int a = 9;
int b = 11;
System.out.println("2진수 a: " + Integer.toBinaryString(a));
System.out.println("2진수 b: " + Integer.toBinaryString(b));
System.out.println("2진수 not 연산(a): " + Integer.toBinaryString(~a));
System.out.println("2진수 not 연산(b): " + Integer.toBinaryString(~b));
System.out.println("10진수 not 연산(a): " + ~a);
System.out.println("10진수 not 연산(b): " + ~b);
}
}

<<연산
ex)
3 << 2
0000 0011 << 2
-> 0000 1100
-> 3 * 2^2 = 12
>>연산
ex)
16 >> 3
0001 0000 >> 3
-> 0000 0010
-> 16 / 2^3 = 2
코드
public class Bit {
public static void main(String[] args) {
System.out.println("3 << 2");
System.out.println("10진수 shift 연산: " + (3 << 2));
System.out.println("2진수 shift 연산: " + Integer.toBinaryString((3 <<2)));
System.out.println();
// 0001 0000 -(오른쪽으로 3 shift)-> 0000 0010
System.out.println("16 >> 3");
System.out.println("10진수 shift 연산(양수, 부호o): " + (16 >> 3));
System.out.println("2진수 shift 연산(양수, 부호o): " + Integer.toBinaryString((16 >> 3)));
System.out.println();
// 0001 0000 -(1의 보수)-> 1110 1111 -(+1)-> 1111 0000
// 1111 0000(-16) -(오른쪽으로 3 shift)-> 1111 1110(-2)
// 1111 1110(-2) -(1의 보수)-> 0000 0001 -(+1)-> 0000 0010(2)
System.out.println("-16 >> 3");
System.out.println("10진수 shift 연산(음수, 부호o): " + (-16 >> 3));
System.out.println("2진수 shift 연산(음수, 부호o): " + Integer.toBinaryString((-16 >> 3)));
}
}

ex)
16 >>> 3
0001 0000 >>> 3
-> 0000 0010
-> ...1111 1101
-> ...1111 1110
-> 536870910
코드
public class Bit {
public static void main(String[] args) {
// 0001 0000 -(오른쪽으로 3 shift)-> 0000 0010
System.out.println("16 >>> 3");
System.out.println("10진수 shift 연산(양수, 부호x): " + (16 >>> 3));
System.out.println("2진수 shift 연산(양수, 부호x): " + Integer.toBinaryString((16 >>> 3)));
System.out.println();
// 부호 고려 안함. 무조건 양수만.
// 0001 0000 -(1의 보수)-> 1110 1111 -(+1)-> 1111 0000
// 1111 0000(-16) -(오른쪽으로 3 shift)-> ...1111 1110(536870910)
System.out.println("-16 >>> 3");
System.out.println("10진수 shift 연산(음수, 부호x): " + (-16 >>> 3));
System.out.println("2진수 shift 연산(음수, 부호x): " + Integer.toBinaryString((-16 >>> 3)));
}
}
