• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why -1 >> 1 = -1 ?  RSS feed

 
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!