What does a bitwise shift (left or right) do and what is it used for?

前端 未结 9 2032
傲寒
傲寒 2020-12-02 05:00

I\'ve seen the operators >> and << in various code that I\'ve looked at (none of which I actually understood), but I\'m just wondering

相关标签:
9条回答
  • 2020-12-02 05:30

    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 a huge data set.

    For example, calculating the 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 a bitwise left shift operation would be like:

    value = 1 << n;
    

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

    0 讨论(0)
  • 2020-12-02 05:33

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

    For example, x = x * 2; can also be written as x<<1 or x = x*8 can be written as x<<3 (since 2 to the power of 3 is 8). Similarly x = x / 2; is x>>1 and so on.

    0 讨论(0)
  • 2020-12-02 05:33

    The bit shift operators are more efficient as compared to the / or * operators.

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

    0 讨论(0)
  • 2020-12-02 05:37

    Left shift: It is equal to the product of the value which has to be shifted and 2 raised to the power of number of bits to be shifted.

    Example:

    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
    

    Right shift: It is equal to quotient of value which has to be shifted by 2 raised to the power of number of bits to be shifted.

    Example:

    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
    
    0 讨论(0)
  • 2020-12-02 05:39

    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);
    
    0 讨论(0)
  • 2020-12-02 05:42

    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.
    0 讨论(0)
提交回复
热议问题