• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why -1 >> 1 = -1 ?

 
Daniel Liu
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could some some explain me why -1 >> 1 = -1 ?
 
Khalid Bou-Rabee
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<code>-1 >> 1 = -1</code>,
<code>-1 in binary: 11111111 (some number of ones)</code>
Shift it once to the right (<code>>></code> retains sign and thus imports 1's from the left) and you still have -1.
This might seem convusing because you might wonder how a bunch of 1's can equal -1. Will this happens because the numbers are counted from 0 backwards up until <code>2^32-1<code> (for <code>int</code>).
------------------
\\ //
\// irucidal
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!