자바 오른쪽 시프트 - >>>

Rashmi Patidar 2023년10월12일
자바 오른쪽 시프트 - >>>

Java 언어에서 >>>는 종종 unsigned right bitshift 연산자로 알려져 있습니다. 부호 있는 연산자와 달리 항상 후행 위치가 0 값으로 채워지도록 허용합니다. 예제를 통해 다음 작업을 이해합시다.

와 b인 두 수를 고려하십시오. 주어진 두 값은 아래와 같습니다.

a = 20
b = -20
a = 00000000000000000000000000010100
b = 11111111111111111111111111101100

비트 오른쪽 시프트 연산자의 사용 사례는 값 나누기 또는 2로 변수입니다.

이제 부호 없는 오른쪽 시프트 연산자, 즉 a>>>1을 적용해 보겠습니다. 연산자는 내부적으로 변수의 모든 비트를 오른쪽으로 이동합니다. 후행 위치를 0 값으로 채웁니다.

아래는 같은 내용을 이해하기 위한 코드 블록입니다.

public class RightShiftOperator {
  public static void main(String[] args) {
    System.out.println("The value of a and b before >>> operator");
    int x = 20;
    int y = -20;
    System.out.println(Integer.toBinaryString(x));
    System.out.println(Integer.toBinaryString(y));
    System.out.println("The value of a and b after applying >>> operator");
    System.out.println(Integer.toBinaryString(x >>> 1));
    System.out.println(Integer.toBinaryString(y >>> 1));
    int divX = x >>> 1;
    int divY = y >>> 1;
    System.out.println("Half the value of x: " + divX);
    System.out.println("Half the value of y: " + divY);
  }
}

위의 코드 블록에서 변수 ab는 각각 값 20과 -20으로 초기화됩니다.

Integer 클래스의 toBinaryString() 함수는 인쇄 스트림 메서드에 적용됩니다.

그 기능은 정수 변수를 이진 문자열로 변환하도록 변환하는 것입니다. 이 방법은 Java 1.2 버전에서 사용할 수 있습니다.

Integer 클래스에는 기본 int 값을 해당 래퍼 객체로 변환하는 기능이 있으므로 기본 값에 대한 래퍼 역할을 합니다.

메소드의 입력은 String 값으로 변환될 int 변수입니다. 함수에서 전달되는 매개변수는 연산자와 함께 변수입니다.

마지막으로 작업이 수행된 변수가 인쇄됩니다.

>>> 연산자를 사용한 코드의 출력은 다음과 같습니다.

The value of a and b before >>> operator
00000000000000000000000000010100
11111111111111111111111111101100
The value of a and b after applying >>> operator
00000000000000000000000000001010
01111111111111111111111111110110
Half the value of x: 10
Half the value of y: 2147483638
Rashmi Patidar avatar Rashmi Patidar avatar

Rashmi is a professional Software Developer with hands on over varied tech stack. She has been working on Java, Springboot, Microservices, Typescript, MySQL, Graphql and more. She loves to spread knowledge via her writings. She is keen taking up new things and adopt in her career.

LinkedIn

관련 문장 - Java Operator