프로그래밍/JAVA Spring

[JAVA 자바] 비트연산자 & | ^ ~ << >>

hectick 2022. 1. 21. 22:29

비트연산자

비트 연산은 0과 1로 표현된 이진수에 관한 연산으로, 비트연산자는 피연산자를 비트단위로 연산한다.

실수형(float, double)을 제외한 모든 기본형에 사용 가능하다.

 

OR 연산자( | )

두 개의 피연산자의 해당 비트 중 어느 한 쪽이 1이면 1을 반환하고, 아니면 0을 반환한다.

public class Main {
	public static void main(String[] args) {
		int A = 13;
		int B = 4;
		
		System.out.println("십진수 : "+ (A) + " -> 이진수 :" + Integer.toBinaryString(A));
		System.out.println("십진수 : "+ (B) + " -> 이진수 :" + Integer.toBinaryString(B));
		System.out.println("13 | 4 연산 결과는 이진수로 " + Integer.toBinaryString(A|B));
	}
}
< 출력결과 >
십진수 : 13 -> 이진수 :1101
십진수 : 4 -> 이진수 :100
13 | 4 연산 결과는 이진수로 1101

 

AND 연산자( & )

두 개의 피연산자의 해당 비트가 양 쪽 모두 1이면 1을 반환하고, 아니면 0을 반환한다.

public class Main {
	public static void main(String[] args) {
		int A = 13;
		int B = 4;
		
		System.out.println("십진수 : "+ (A) + " -> 이진수 :" + Integer.toBinaryString(A));
		System.out.println("십진수 : "+ (B) + " -> 이진수 :" + Integer.toBinaryString(B));
		System.out.println("13 & 4 연산 결과는 이진수로 " + Integer.toBinaryString(A&B));
	}
}
< 출력결과 >
십진수 : 13 -> 이진수 :1101
십진수 : 4 -> 이진수 :100
13 & 4 연산 결과는 이진수로 100

 

XOR 연산자( ^ )

두 개의 피연산자의 해당 비트의 값이 서로 다를 때 1을 반환하고, 같으면 0을 반환한다.

public class Main {
	public static void main(String[] args) {
		int A = 13;
		int B = 4;
		
		System.out.println("십진수 : "+ (A) + " -> 이진수 :" + Integer.toBinaryString(A));
		System.out.println("십진수 : "+ (B) + " -> 이진수 :" + Integer.toBinaryString(B));
		System.out.println("13 ^ 0 연산 결과는 이진수로 " + Integer.toBinaryString(A^0));
		System.out.println("13 ^ 4 연산 결과는 이진수로 " + Integer.toBinaryString(A^B));
		System.out.println("13 ^ 4 ^ 13 연산 결과는 이진수로 " + Integer.toBinaryString(A^B^A));
		System.out.println("13 ^ 4 ^ 4 연산 결과는 이진수로 " + Integer.toBinaryString(A^B^B));
	}
}
< 출력결과 >
십진수 : 13 -> 이진수 :1101
십진수 : 4 -> 이진수 :100
13 ^ 0 연산 결과는 이진수로 1101
13 ^ 4 연산 결과는 이진수로 1001
13 ^ 4 ^ 13 연산 결과는 이진수로 100
13 ^ 4 ^ 4 연산 결과는 이진수로 1101

 

NOT 연산자( ~ )

0은 1로, 1은 0으로 반전시킨다.

public class Main {
	public static void main(String[] args) {
		int A = 14;
		
		System.out.println("십진수 : "+ (A) + " -> 이진수 :" + Integer.toBinaryString(A));
		System.out.println("~14 연산 결과는 이진수로 " + Integer.toBinaryString(~A));
	}
}
< 출력결과 >
십진수 : 14 -> 이진수 :1110
~14 연산 결과는 이진수로 11111111111111111111111111110001

 

Left shift 연산자( << )

지정된 수만큼 모든 비트를 왼쪽으로 이동시킨다.

이동 후 오른쪽에 생기는 빈 자리는 0으로 채운다.

public class Main {
	public static void main(String[] args) {
		int A = 14;
		
		System.out.println("십진수 : "+ (A) + " -> 이진수 :" + Integer.toBinaryString(A));
		System.out.println("14 << 2 연산 결과는 이진수로 " + Integer.toBinaryString(A<<2));
	}
}
< 출력결과 >
십진수 : 14 -> 이진수 :1110
14 << 2 연산 결과는 이진수로 111000

 

Right shift 연산자( >> )

지정된 수만큼 모든 비트를 오른쪽으로 이동시킨다.

public class Main {
	public static void main(String[] args) {
		int A = 14;
		
		System.out.println("십진수 : "+ (A) + " -> 이진수 :" + Integer.toBinaryString(A));
		System.out.println("14 >> 2 연산 결과는 이진수로 " + Integer.toBinaryString(A>>2));
	}
}
< 출력결과 >
십진수 : 14 -> 이진수 :1110
14 >> 2 연산 결과는 이진수로 11