0

Possible Duplicate:
Left shifting with a negative shift count

On a 16-bit compiler, why does 32<<-3 or 32>>-1 result in 0? what is the major reason for such a behaviour

Community
  • 1
  • 1
Rishabh
  • 3,752
  • 4
  • 47
  • 74
  • 1
    This is undefined behavior, and will vary depending on which c implementation you use. But the question's also a duplicate. – Don Roby Jul 04 '12 at 11:13
  • 1
    "The results are undefined if the right operand of a shift expression is negative" : http://msdn.microsoft.com/en-us/library/336xbhcz(v=vs.80).aspx – Denys Séguret Jul 04 '12 at 11:14

1 Answers1

2

From K&R:

The shift operators << and >> perform left and right shifts of their left operand by the number of bit positions given by the right operand, which must be non-negative

nav_jan
  • 2,473
  • 4
  • 24
  • 42
  • That doesn't really answer the question. It says that the right operand must be non-negative but doesn't say what happens if you do make it negative. – flight Jul 04 '12 at 11:16
  • @quasiverse what happens is exactly what the person who asked the question is experiencing .. confusion – nav_jan Jul 04 '12 at 11:20
  • @quasiverse In doubt, you invoke UB. – glglgl Jul 04 '12 at 11:24