📚 Study/Java
JAVA :: Test024_비트 단위 연산자(시프트)
bono-hye
2023. 9. 7. 22:47
시프트 연산자
- >> 오른쪽으로 자리 이동
- << 왼쪽으로 자리 이동
- 이동할 자리 수가 배열을 초과하는 경우 : 다시 자기 자리 찾아가기 전까지는 잠복 (0출력)
public class Test025
{
public static void main(String[] args)
{
int x = 128; // 00000000 00000000 00000000 10000000
System.out.printf("x << 3 : %d\n", x << 3);
// 00000000 00000000 00000100 00000000 → 1024
System.out.printf("x * 8 : %d\n", x * 8);
//--==>> x << 3 : 1024
// x * 8 : 1024
System.out.printf("x >> 3 : %d\n", x >> 3);
// 00000000 00000000 00000000 00010000 → 16
System.out.printf("x / 8 : %d\n", x / 8);
//--==>> x >> 3 : 16
// x / 8 : 16
System.out.printf("x << 24 : %d\n", x << 24);
// 10000000 00000000 00000000 00000000 → ≒ -21억
//--==>> x << 24 : -2147483648
System.out.printf("x << 25 : %d\n", x << 25);
System.out.printf("x << 26 : %d\n", x << 26);
System.out.printf("x << 27 : %d\n", x << 27);
System.out.printf("x << 28 : %d\n", x << 28);
System.out.printf("x << 29 : %d\n", x << 29);
System.out.printf("x << 30 : %d\n", x << 30);
System.out.printf("x << 31 : %d\n", x << 31);
//--==>> x << 25 : 0
// x << 26 : 0
// x << 27 : 0
// x << 28 : 0
// x << 29 : 0
// x << 30 : 0
// x << 31 : 0
System.out.printf("x << 32 : %d\n", x << 32); // 원래 자기 자리 찾아가기 전까지는 잠복(0)
//--==>> x << 32 : 128
}
}
[Test024 실행결과]
x << 3 : 1024
x * 8 : 1024
x >> 3 : 16
x / 8 : 16
x << 24 : -2147483648
x << 25 : 0
x << 26 : 0
x << 27 : 0
x << 28 : 0
x << 29 : 0
x << 30 : 0
x << 31 : 0
x << 32 : 128
계속하려면 아무 키나 누르십시오 . . .