development

Go << 및 >> 연산자

big-blog 2020. 8. 29. 12:23
반응형

Go << 및 >> 연산자


누군가가 나에게의 사용 설명시겠습니까 <<>>이동에를? 다른 언어와 비슷하다고 생각합니다.


수퍼 (아마도 오버) 단순화 된 정의는 <<"2 배"에 사용되며 "2 >>로 나누기"에 사용되며 그 다음의 숫자는 몇 번입니다.

그래서 n << x"n x 2, x x"입니다. 그리고 y >> z"y를 2로 나눈 값, z 배"입니다.

예를 들어 1 << 5"1 x 2, 5 x"또는 32입니다. 그리고 32 >> 5"32를 2로 나눈 값, 5 x"또는 1입니다.

다른 모든 답변은 더 기술적 인 정의를 제공하지만 아무도 그것을 정말로 퉁명스럽게 배치하지 않았고 나는 당신이 그것을 원할 것이라고 생각했습니다.


http://golang.org/doc/go_spec.html 의 사양에서 적어도 정수의 경우 이진 시프트 인 것 같습니다. 예를 들어, 바이너리 0b00001000 >> 1은 0b00000100이고 0b00001000 << 1은 0b00010000입니다.


Go는 이진 정수에 대해 0b 표기법을 받아들이지 않는 것 같습니다. 나는 단지 예를 위해 그것을 사용하고 있었다. 십진수에서 8 >> 1은 4이고 8 << 1은 16입니다. 왼쪽으로 1만큼 이동하는 것은 2로 곱하는 것과 같고 오른쪽으로 1로 이동하는 것은 2로 나누고 나머지를 버리는 것과 같습니다.


<< 및 >> 연산자는 Go 산술 연산자 입니다.

<<   left shift             integer << unsigned integer
>>   right shift            integer >> unsigned integer

시프트 연산자는 오른쪽 피연산자에서 지정한 시프트 수만큼 왼쪽 피연산자를 시프트합니다. 왼쪽 피연산자가 부호있는 정수이면 산술 시프트를 구현하고 부호없는 정수이면 논리 시프트를 구현합니다. 시프트 카운트는 부호없는 정수 여야합니다. 시프트 수에는 상한선이 없습니다. 시프트는 시프트 카운트 n에 대해 왼쪽 피연산자가 1 씩 n 번 시프트 된 것처럼 작동합니다. 결과적으로 x << 1은 x * 2와 같고 x >> 1은 x / 2와 같지만 음의 무한대로 잘립니다.


그것들은 기본적으로 산술 연산자 이며 다른 언어에서도 마찬가지입니다. 여기에 기본적인 PHP, C, Go 예제가 있습니다.

가다

package main

import (
    "fmt"
)

func main() {
    var t , i uint
    t , i = 1 , 1

    for i = 1 ; i < 10 ; i++ {
        fmt.Printf("%d << %d = %d \n", t , i , t<<i)
    }


    fmt.Println()

    t = 512
    for i = 1 ; i < 10 ; i++ {
        fmt.Printf("%d >> %d = %d \n", t , i , t>>i)
    }

}

GO 데모

#include <stdio.h>
int main()
{

    int t = 1 ;
    int i = 1 ;

    for(i = 1; i < 10; i++) {
        printf("%d << %d = %d \n", t, i, t << i);
    }

        printf("\n");

    t = 512;

    for(i = 1; i < 10; i++) {
        printf("%d >> %d = %d \n", t, i, t >> i);
    }    

  return 0;
}

C 데모

PHP

$t = $i = 1;

for($i = 1; $i < 10; $i++) {
    printf("%d << %d = %d \n", $t, $i, $t << $i);
}

print PHP_EOL;

$t = 512;

for($i = 1; $i < 10; $i++) {
    printf("%d >> %d = %d \n", $t, $i, $t >> $i);
}

PHP 데모

그들은 모두 출력합니다

1 << 1 = 2 
1 << 2 = 4 
1 << 3 = 8 
1 << 4 = 16 
1 << 5 = 32 
1 << 6 = 64 
1 << 7 = 128 
1 << 8 = 256 
1 << 9 = 512 

512 >> 1 = 256 
512 >> 2 = 128 
512 >> 3 = 64 
512 >> 4 = 32 
512 >> 5 = 16 
512 >> 6 = 8 
512 >> 7 = 4 
512 >> 8 = 2 
512 >> 9 = 1 

Go's << and >> are similar to shifts (that is: division or multiplication by a power of 2) in other languages, but because Go is a safer language than C/C++ it does some extra work when the shift count is a number.

Shift instructions in x86 CPUs consider only 5 bits (6 bits on 64-bit x86 CPUs) of the shift count. In languages like C/C++, the shift operator translates into a single CPU instruction.

The following Go code

x := 10
y := uint(1025)  // A big shift count
println(x >> y)
println(x << y)

prints

0
0

while a C/C++ program would print

5
20

<< is left shift. >> is sign-extending right shift when the left operand is a signed integer, and is zero-extending right shift when the left operand is an unsigned integer.

To better understand >> think of

var u uint32 = 0x80000000;
var i int32 = -2;

u >> 1;  // Is 0x40000000 similar to >>> in Java
i >> 1;  // Is -1 similar to >> in Java

So when applied to an unsigned integer, the bits at the left are filled with zero, whereas when applied to a signed integer, the bits at the left are filled with the leftmost bit (which is 1 when the signed integer is negative as per 2's complement).


In decimal math, when we multiply or divide by 10, we effect the zeros on the end of the number.

In binary, 2 has the same effect. So we are adding a zero to the end, or removing the last digit

참고URL : https://stackoverflow.com/questions/5801008/go-and-operators

반응형