Wednesday, July 30, 2014

#75 – New Bits When Shifting | 2,000 Things You Should Know About C#

For future reference:

#75 – New Bits When Shifting

When doing bitwise shifting using the << and >> operators, the data value being shifted will always shift new bits in–from the left when right shifting, or from the right when left shifting.  The value of the new bits depend on the direction of the shift and the data type being shifted.
  • Right shift
    • If shifting int or long, new bits at left match sign of the value
    • If shifting uint or ulong, new bits at left are 0-valued
  • Left shift
    • New bits at right are 0-valued
Shift operations can never result in an overflow.

No comments: