• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Operator >>>

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
128>>1 gives 64, but why??
I think if 128 = 10000000
10000000>>>1 = 11000000
the restul will be 192 because the operator >> added to the left side will be the same value as the leading sign bit.
thanks in advanced
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
firstly, 128 is NOT "10000000", but instead is
"00000000000000000000000010000000", when you do a ">>", the left most digit is still zero.
secondly, ">>>" doesn't keep the left most digit, so you left most digit is alway filled with a zero.
Wei
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Original post:


<CODE>
Hi!
128>>1 gives 64, but why??
I think if 128 = 10000000
10000000>>>1 = 11000000
the restul will be 192 because the operator >> added to the left side will be the same value as the leading sign bit.
thanks in advanced

</CODE>


I don't exactly understand what you are saying but.
128 in bits is
<code>
00000000 00000000 00000000 10000000
128 >> 1 // this means that the wole bitpattern will move a step to the right. The rightmost bit falls out and disappears. Since the leftmost bit is 0 the bitpattern will add a 0 to the bitfield
that's leaving the bitfield with
00000000 00000000 00000000 01000000 = 64
</code>
Please read the http://www.javaranch.com/campfire/StoryBits.jsp
it will explain bits for you.


------------------
Preparing for the Java 2 Certification exam
 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jordi,
>>, would take the Most Significant Bit(MSB) on the left side and fill the shifts with that bit, while >>> would fill the shifts with zeros irrespective of the MSB.
In bit format, 128 >> 1 is:
<pre>
128 ===> 0000 0000 0000 0000 0000 0000 1000 0000
>>1 ===> 0000 0000 0000 0000 0000 0000 0100 0000 ===> 64
</pre>

In bit format, 128 >>> 1 is:
<pre>
128 ===> 0000 0000 0000 0000 0000 0000 1000 0000
>>>1 ===> 0000 0000 0000 0000 0000 0000 0100 0000 ===> 64
</pre>

Note that the results would be same in both the cases.
-- Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
[This message has been edited by Desai Sandeep (edited July 28, 2001).]
reply
    Bookmark Topic Watch Topic
  • New Topic