• 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

Unsigned Right Shift

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain that the result of -1>>>32 is -1 not 0?
Thanks!
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only the last 5 bits of the right operand are used. That is, the right operand is modded by 32. 32 % 32 = 0, so you aren't shifting bits.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think .. java 2's complement is the reason

1 value is 00000000 00000000 00000000 00000001

-1 value is (as java uses 2's complement)

negate the unsigned value and add 1

11111111 11111111 11111111 11111110 (negate value)
00000000 00000000 00000000 00000001 (one's value)

11111111 11111111 11111111 11111111 (-1 value)

so now shifting 1 by 32 and applying negate

00000000 00000000 00000000 00000001 (one's value)
00000000 00000000 00000000 00000000 (after applying >>>32)

now negate
11111111 11111111 11111111 11111111 which is equal to -1


does that make sense ?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Keith Lynn:
Only the last 5 bits of the right operand are used. That is, the right operand is modded by 32. 32 % 32 = 0, so you aren't shifting bits.


That's correct. And for type long, the last 6 bits are used.

(So note what happens when the right operand is negative. )
[ January 26, 2006: Message edited by: marc weber ]
 
ven kaar
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marc and Keith are rite, mod operator is used for 32 and above in case of int, 64 and above in case of long.

so in essence 32%32 stands 0 no shifting occurs
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also remember that this topic is on the 1.4 exam but it's NOT on the 5.0 exam.
 
Slideshow boring ... losing consciousness ... just gonna take a quick nap on this tiny ad ...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic