본문 바로가기

자바/자바 기초

비트단위 부정 연산자

예제)


public class Test3 {

public static void main(String[] args) {

byte a=24,b,d,e;


b=(byte)~a; // 비트단위 부정연산자. 설정된 값에 부호가 반대로되며 절대값은 +1가 된다.


/*

2진수

a : 0001 1000  

b : 1110 0111

*/


System.out.println("a:"+a);

System.out.println("b:"+b);


byte c;

a=12;  // 0000 1100

b=6;    // 0000 0110


c=(byte)(a&b);  // 0000 0100 -> 4

d=(byte)(a|b);  // 0000 1110 -> 14

e=(byte)(a^b);  // 0000 1010 -> 10

System.out.println(c);

System.out.println(d);

System.out.println(e);


}

}


결과)


a:24

b:-25

4

14

10