IT TIP

비트 시프트 (왼쪽 또는 오른쪽)의 기능과 용도는 무엇입니까?

itqueen 2020. 11. 23. 20:45
반응형

비트 시프트 (왼쪽 또는 오른쪽)의 기능과 용도는 무엇입니까?


연산자 >>제가 <<살펴본 다양한 코드 (실제로는 이해하지 못함)를 보았지만 실제로 무엇을하는지, 실제 용도가 무엇인지 궁금합니다.

편집하다

시프트가 x * 2and와 같은 경우 x / 2실제로 *and /연산자를 사용하는 것과 실제 차이점은 무엇 입니까? 성능 차이가 있습니까?


다음은 이동을 포함하여 비트 연산을 실행할 수 있는 애플릿 입니다.

당신은 비트 모음을 가지고 있고 그들 중 일부를 그들의 범위를 넘어서 이동합니다.

1111 1110 << 2 
1111 1000 

오른쪽부터 새로운 0으로 채워집니다. :)

0001 1111 >> 3 
0000 0011 

왼쪽에서 채워집니다. 특별한 경우는 선행 1입니다. 언어 및 데이터 유형에 따라 종종 음수 값을 나타냅니다. 오른쪽으로 이동하면 첫 번째 비트가 그대로 유지되기를 원합니다.

1100 1100 >> 1
1110 0110 

그리고 여러 교대에 걸쳐 보존됩니다.

1100 1100 >> 2
1111 0011

첫 번째 비트를 보존하지 않으려면 (Java, Scala, C ++, C afaik 등에서) 삼중 부호 연산자를 사용합니다.

1100 1100 >>> 1
0110 0110 

의미가 없기 때문에 다른 방향으로는 동등하지 않습니다. 아마도 당신의 매우 특별한 상황에서는 아니지만 일반적으로는 그렇지 않습니다.

수학적으로 왼쪽 시프트는 * = 2이고, 왼쪽 시프트 2 개는 * = 4입니다. 오른쪽 시프트는 / = 2 등입니다.


2의 거듭 제곱으로 곱하기위한 왼쪽 비트 시프트와 2의 모든 제곱으로 나누기 위해 오른쪽 비트 시프 팅. 예를 들어 x = x * 2;로도 쓸 수 x<<1또는 x = x*8과 같이 쓸 수있다 x<<3((3)의 전원 (2)이기 때문에 8). 이와 유사 x = x / 2;하다 x>>1등등.


왼쪽 시프트

x = x * 2^value (정상 작동)

x << value (비트 단위 연산)


x = x * 16(와 동일 2^4)

왼쪽 시프트는 다음과 같습니다. x = x << 4

오른쪽 시프트

x = x / 2^value (일반적인 산술 연산)

x >> value (비트 단위 연산)


x = x / 8(와 동일 2^3)

오른쪽 시프트는 다음과 같습니다. x = x >> 3


왼쪽 시프트 : 시프트해야하는 값의 곱과 동일하고 시프트 할 비트 수의 제곱으로 2를 올립니다.

예 :

1<<3
0000 0001  ---> 1
Shift by 1 bit
0000 0010 ----> 2 which is equal to 1*2^1
Shift By 2 bits
0000 0100 ----> 4 which is equal to 1*2^2
Shift by 3 bits
0000 1000 ----> 8 which is equal to 1*2^3

오른쪽 시프트 : 시프트 할 비트 수의 거듭 제곱으로 2만큼 시프트해야하는 값의 몫과 같습니다.

예 :

8>>3
0000 1000  ---> 8 which is equal to 8/2^0
Shift by 1 bit
0000 0100 ----> 4 which is equal to 8/2^1
Shift By 2 bits
0000 0010 ----> 2 which is equal to 8/2^2
Shift by 3 bits
0000 0001 ----> 1 which is equal to 8/2^3

The bit shift operators are more efficient as compared to / or * operator. In computer architecture, divide(/) or multiply(*) take more than 1 time unit and register to compute result, while, bit shift operator, is just one one register and one time unit computation.


Left bit shifting to multiply by any power of two. Right bit shifting to divide by any power of two.

x = x << 5; // Left shift
y = y >> 5; // Right shift

In C/C++ it can be written as,

#include <math.h>

x = x * pow(2, 5);
y = y / pow(2, 5);

Some examples:

  • Bit operations for example converting to and from base64 (which is 6 bits instead of 8)
  • doing power of 2 operations (1 << 4 equal to 2^4 i.e. 16)
  • Writing more readable code when working with bits. For example, defining constants using 1 << 4 or 1 << 5 is more readable.

Yes, I think performance wise you might find a difference as bitwise left and right shift operations can be performed with a complexity of o(1) with huge data set.

For eg Calculating power of 2 ^ n:-

int value = 1;
while (exponent<n)
    {
       //print out current power of 2
        value =value *2; // equivalent machine level left shift bit wise operation
        exponent++;
         }
    }

Similar code with bitwise left shift operation would be like:

value = 1 << n;

Moreover, performing bit-wise operation is like exacting replica of user level mathematical operations (which is the final machine level instructions processed by the micro controller and processor).


Here is an example:

#include"stdio.h"
#include"conio.h"

void main()
{
    int rm,vivek;
    clrscr();
    printf("enter the any numbers\t(e.g)1,2,5");
    scanf("%d",&rm);//rm=5(0101)<<2(two step add zero's)so,value is 10100
    printf("this lift shitf value%d=%d",rm,rm<<4);
    printf("this right shitf value%d=%d",rm,rm>>2);
    getch();
}

참고URL : https://stackoverflow.com/questions/6385792/what-does-a-bitwise-shift-left-or-right-do-and-what-is-it-used-for

반응형