Are the shift operators (<<, >>) arithmetic or logical in C?

后端 未结 11 1412
粉色の甜心
粉色の甜心 2020-11-22 07:52

In C, are the shift operators (<<, >>) arithmetic or logical?

相关标签:
11条回答
  • 2020-11-22 08:24

    GCC does

    1. for -ve - > Arithmetic Shift

    2. For +ve -> Logical Shift

    0 讨论(0)
  • 2020-11-22 08:29

    When shifting left, there is no difference between arithmetic and logical shift. When shifting right, the type of shift depends on the type of the value being shifted.

    (As background for those readers unfamiliar with the difference, a "logical" right shift by 1 bit shifts all the bits to the right and fills in the leftmost bit with a 0. An "arithmetic" shift leaves the original value in the leftmost bit. The difference becomes important when dealing with negative numbers.)

    When shifting an unsigned value, the >> operator in C is a logical shift. When shifting a signed value, the >> operator is an arithmetic shift.

    For example, assuming a 32 bit machine:

    signed int x1 = 5;
    assert((x1 >> 1) == 2);
    signed int x2 = -5;
    assert((x2 >> 1) == -3);
    unsigned int x3 = (unsigned int)-5;
    assert((x3 >> 1) == 0x7FFFFFFD);
    
    0 讨论(0)
  • 2020-11-22 08:31

    Here are functions to guarantee logical right shift and arithmetic right shift of an int in C:

    int logicalRightShift(int x, int n) {
        return (unsigned)x >> n;
    }
    int arithmeticRightShift(int x, int n) {
        if (x < 0 && n > 0)
            return x >> n | ~(~0U >> n);
        else
            return x >> n;
    }
    
    0 讨论(0)
  • 2020-11-22 08:35

    When you do - left shift by 1 you multiply by 2 - right shift by 1 you divide by 2

     x = 5
     x >> 1
     x = 2 ( x=5/2)
    
     x = 5
     x << 1
     x = 10 (x=5*2)
    
    0 讨论(0)
  • 2020-11-22 08:43

    Left shift <<

    This is somehow easy and whenever you use the shift operator, it is always a bit-wise operation, so we can't use it with a double and float operation. Whenever we left shift one zero, it is always added to the least significant bit (LSB).

    But in right shift >> we have to follow one additional rule and that rule is called "sign bit copy". Meaning of "sign bit copy" is if the most significant bit (MSB) is set then after a right shift again the MSB will be set if it was reset then it is again reset, means if the previous value was zero then after shifting again, the bit is zero if the previous bit was one then after the shift it is again one. This rule is not applicable for a left shift.

    The most important example on right shift if you shift any negative number to right shift, then after some shifting the value finally reach to zero and then after this if shift this -1 any number of times the value will remain same. Please check.

    0 讨论(0)
  • 2020-11-22 08:43

    gcc will typically use logical shifts on unsigned variables and for left-shifts on signed variables. The arithmetic right shift is the truly important one because it will sign extend the variable.

    gcc will will use this when applicable, as other compilers are likely to do.

    0 讨论(0)
提交回复
热议问题