Search code examples
cbinarybit-manipulationbit-shift

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


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


Solution

  • According to K&R 2nd edition the results are implementation-dependent for right shifts of signed values.

    Wikipedia says that C/C++ 'usually' implements an arithmetic shift on signed values.

    Basically you need to either test your compiler or not rely on it. My VS2008 help for the current MS C++ compiler says that their compiler does an arithmetic shift.