[java] 비트연산

한지개·2023년 7월 10일

java

목록 보기
8/9

AND 연산 (&)

입력(A)입력(B)출력(F)
000
010
100
111

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진수(문자열)


OR 연산 (|)

입력(A)입력(B)출력(F)
000
011
101
111

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));
    }
}


XOR 연산 (^)

입력(A)입력(B)출력(F)
000
011
101
110

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));
    }
}


NOT 연산 (~)

입력(A)출력(F)
01
10

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);
    }
}


SHIFT 연산(1) (<<, >>)

<<연산

  • 2진수의 각자리를 왼쪽으로 N칸 밀고 최하위 비트를 0으로 채워주는 연산

ex)
3 << 2

0000 0011 << 2
-> 0000 1100
-> 3 * 2^2 = 12


>>연산

  • 2진수의 각자리를 오른쪽으로 N칸 밀고 밀면서 사라지는 자리수는 삭제
  • 최상위 비트는 동일한 부호비트로 채움(음수일 경우 1, 양수일 경우 0)

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)));
    }
}

SHIFT 연산(2) (>>>)

  • 2진수의 각자리를 오른쪽으로 N칸 밀고 밀면서 사라지는 자리수는 삭제
  • 최상위 비트는 항상 0으로 채움(양수만 고려)
  • >>>연산시 무조건 양수만 출력됨

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)));
    }
}

profile
평생 소원이 누룽지

0개의 댓글