• 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 bit shift operator

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(-1>>>1) 2147483647
(-1>>31) 1
(-1>>>32) -1
(-1>>>33) 2147483647
Do the above results follow any logic??
Thanks in advance for the reply.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by p intelli:
(-1>>>1) 2147483647
(-1>>31) 1
(-1>>>32) -1
(-1>>>33) 2147483647
Do the above results follow any logic??
Thanks in advance for the reply.



Well, I can see that the first and the last do follow the same logic - shifting for 33 is actually shifting for 1. Shifting for 32 is not shifting at all. Mind you, you're using unsigned operator!
And as for 31 let's see:
In order to represent -1 we'll start from
1=0000 0001 (spread this to 32 bits for ints)
apply ~=1111 1110
add 1 =1111 1111
So -1 is actually 0xFFFFFFFF. You can shift it whatever you like but it will always stay -1:
-1>>31 = -1
 
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello again P intelli,

(-1>>>1) 2147483647
(-1>>31) 1
(-1>>>32) -1
(-1>>>33) 2147483647
Do the above results follow any logic??


OK, bert will be impressed here. Bert, I loved your answer, I had a good chuckle to myself!!
>> shifts bits but uses the signed bit, most left bit if a 1 keep all shifted bits a 1, if a 0 keep all shifted bits a 0.
>>> shift the bits to the right but make all the added bits a 0, no matter what you had.
1:
-1 looks like this in bits
11111111 11111111 11111111 11111111 >>> 1 is:
01111111 11111111 11111111 11111111 some large positive int 2147483647
2:
11111111 11111111 11111111 11111111 >> 31 signed bitshift so you get
11111111 11111111 11111111 11111111 = -1 not 1 as you have said
3:
11111111 11111111 11111111 11111111 >>> 32 so like saying >>> 0
11111111 11111111 11111111 11111111 = -1 cause you aint shifting anything
4:
11111111 11111111 11111111 11111111 >>> 33 is 32%33 = 1 so >>> 1
01111111 11111111 11111111 11111111 = very big int like 2147483647
hope this helps
Davy
[ February 27, 2004: Message edited by: Davy Kelly ]
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bert Bates:
yes


Really!!! Are you sure?
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vicken
I hope you have finished your certification. I am waiting for the party that you promised long back(Hope you remember) :roll:
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Party still stands Pradeep as i promised, and the good news is that my exam is this Sunday (two days from now -- Oh yeeeeeah). Until then here is a beer it's on the house.
 
Yan Lee
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello and thanks to all who have made an honest effort to answer my question.
After reading your responses, these is what that I have understood:
1. for right hand operand greater than 32 is equivalent to shifting with
right hand operand%32
2. for right hand operand equal to 32, the number remains the same
3. for anything less than 32, the bits are shifted right by that number and
0's are added to the left.
Thanks once again.
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
p -
Sorry, I just couldn't resist
Hope you got some good answers here, (thanks Davy).
A couple of key facts to remember:
- you MUST understand two's complement
- you can only shift ints 0-31 places, shifting 32+ is like shifting X % 32
- for longs it's 0-63 places, X % 64

p.s. - Hey p, the only rules around here are to be nice, and to use your real name, is "p intelli" your real name? thanks!
[ February 27, 2004: Message edited by: Bert Bates ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bert Bates:

- you can only shift ints 0-31 places, shifting 32+ is like shifting X % 32
- for longs it's 0-63 places, X % 64


I have further explained this (with a reference to the JLS) in your question about shifting by a negative number. You can look there for more info.
reply
    Bookmark Topic Watch Topic
  • New Topic