Java의 부호 없는 및 부호 있는 오른쪽 비트 시프트 연산자

MD Aminul Islam 2023년10월12일
Java의 부호 없는 및 부호 있는 오른쪽 비트 시프트 연산자

이 자습서는 Java의 부호 있는 오른쪽 비트 시프트 연산자와 부호 없는 오른쪽 비트 시프트 연산자에 대해 교육합니다. 또한 코드 예제를 통해 사용법을 보여줍니다.

Java의 부호 없는 및 부호 있는 오른쪽 비트 시프트 연산자

다른 프로그래밍 언어와 달리 Java는 두 개의 오른쪽 비트 시프트 연산자를 지원합니다.

  1. 기호 >>로 표시되는 부호 있는 오른쪽 비트 시프트.
  2. >>> 기호로 표시되는 부호 없는 오른쪽 비트 시프트.

C, C++ 등과 같은 다른 프로그래밍 언어에는 Signed right 비트 시프트 연산자(>>)만 포함되어 있지만 Java에서는 부호 있는 시프트 연산과 부호 없는 시프트 연산을 모두 사용할 수 있습니다.

부호 있는 오른쪽 비트 시프트 연산자와 부호 없는 오른쪽 비트 시프트 연산자의 차이점

부호 있는 오른쪽 이동(>>)은 숫자의 각 비트를 오른쪽으로 이동하고 부호 비트(가장 왼쪽 비트)를 유지합니다. 부호 비트는 숫자의 부호를 예약하는 데 사용됩니다. 부호 비트가 각각 0 또는 1인 경우 숫자는 양수 또는 음수입니다.

반면에 부호 없는 오른쪽 시프트(>>>)도 부호 있는 오른쪽 시프트와 유사한 연산을 수행하지만 차이점은 부호 없는 오른쪽 시프트는 항상 가장 왼쪽 위치를 값이 0으로 채우는 것입니다. 서명되지 않았습니다.

양수에 부호 없는 오른쪽 시프트를 적용하면 부호 있는 오른쪽 시프트와 동일한 결과가 표시되지만 음수를 제공하면 모든 부호 있는 비트가 0으로 대체되므로 결과는 양수가 됩니다.

부호 있는 오른쪽 비트 이동 연산자(>>)의 예:

Let's take a number -11, and we will shift 2 bits towards the right.

+11 in 8-bit form = 0000 1011
1's Complement    = 1111 0100
2's Compelement   =        +1
-----------------------------
2's Complement of -11 = 1111 0101

n = -11 =  1111 0101
Shift 1st bit = 1111 1010
Shift 2nd bit = 1111 1101

1111 1101
       -1
------------------------------
1111 1100 = 1's Complement
0000 0011 = Complement of each bit

So -11 >> 2 = -3 which is in binary 0000 0011.

부호 없는 오른쪽 비트 시프트 연산자(>>>)의 예:

n = 10 = 0000 1010 //the leftmost bit position is filled with '0' as 'n' is positive

Now Shift the '3' bits towards the right; you can do it directly, but let's go one-by-one
n = 0000 1010
Shift 1st bit = 0000 0101
Shift 2nd bit = 0000 0010
Shift 3rd bit = 0000 0001

So, n >>> 3 = 0000 0001 which would be 1 in decimals.

We can also use this formula:(Given decimal number/2^n)
Where 'n' is the required number of shifts, in our case, it is '3'.

So, n >>> 3 = 10/2^3 = 10/8 = 1

이동된 모든 비트는 두 경우 모두 손실됩니다(부호 있는 오른쪽 이동 및 부호 없는 오른쪽 이동).

Java에서 Signed Right Bit Shift(>>) 사용

아래 예에서 숫자에 대해 부호 있는 오른쪽 비트 이동 연산을 수행했습니다.

예제 코드:

public class SignedRightShift {
  public static void main(String args[]) {
    int a = -11;
    System.out.println(a >> 2);

    int b = 4;
    System.out.println(b >> 1);
  }
}

출력:

-3
2

Java에서 부호 없는 오른쪽 비트 시프트(>>>) 사용

아래 예는 숫자에 대한 부호 없는 오른쪽 비트 이동 연산을 보여줍니다.

예제 코드:

public class UnsignedRightShift {
  public static void main(String args[]) {
    // The binary representation of -1 is all "111..1".
    int a = -1;

    // The binary value of 'a >>> 29' is "00...0111"
    System.out.println(a >>> 29);

    // The binary value of 'a >>> 30' is "00...0011"
    System.out.println(a >>> 30);

    // The value of 'a >>> 31' is "00...0001"
    System.out.println(a >>> 31);
  }
}

출력:

7
3
1
MD Aminul Islam avatar MD Aminul Islam avatar

Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.

LinkedIn

관련 문장 - Java Operator