• 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

Shifting (to Sonir... or others if needed)

 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hey there's ome problem now.Though I tried hard to understand the concept of shift operators, I am nt getting it.I tried various resources , RHE, other Tuts, etc..but all in vain,Can you suggest me in this matter..I simply cannot understand (<<,>>,>>> , stuffs..


I wouldn't worry too much about it but here goes:
First, I would read the following article at Javaranch explaining how
to manipulate bits: http://www.javaranch.com/campfire/StoryBits.jsp
Then as you already know all integral types are basically represented in binary by bits, for instance the number 25 is 00011001 in binary.
Now the shift operators allow you to modify those binary-represented numbers by shifting to the left or to the right the bits composing the number. On the left side you put the number and on the rigth side you put the number of shift step you want to achieve, for instance:
in 25 >>> 2 we want to shift the bits representation of 25 two steps to the right. Now to do this, you just take the binary representation of 25 and shift its bit pattern two steps to the right, like this:
00011001 >>> 2 = 00000110 = 6. That is 25 shifted 2 steps to the right is 6.
I think you got it.
Now >> can be tricky. Remember that we just added some 0's on the left side to pad it when shifting to the right side with >>>. With >> it's different, you don't add 0's but the value that is presently the first bit (on the left) before the shift. For instance:
25 >> 2 = 00011001, we have a 0 as the first left bit so we pad 0's like this: 00000110 = 6. Same result as before. But in 11001000 >> 2 we have a 1 as the first bit on the left, so we pad 1's, like this: 11110010.
The main difference between >> and >>> is that we say the first is SIGNED and the second is UNSIGNED. When you deal with negative numbers (that is numbers whose first bit is 1 which indicates a minus sign) the operator >> keeps the sign of the number (because it adds 0 if the first bit is 0, that is the number is and stays positive, and adds 1 if the first bit is 1, that is the number is and stays negative) while >>> doesn't since it adds 0's regardless of what the sign bit is.
A last remark is that the right-hand side of the operator is carefully examined before the shifting. If the number on the left is an int, then the number on the right (that is the shift steps) cannot be bigger than 32 because an
int has 32 bits and it doesn't make sense to shift more than the number can be shifted. As well, if the left-hand side is a long, then the right hand side cannot be bigger than 64 because, again, it doesn't make sense to shift a long for more than 64 bits. So the right hand side is first truncated to 32 (right-hand modulo 32) or 64.
HIH
[ January 19, 2002: Message edited by: Valentin Crettaz ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it right that the unsigned right shift is denoted by >> and the signed right shift is >>>?
I've been trying to learn shift operators over the last couple of days and the sources I've used tell me the exact opposite!
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry you are perfeclty right, it's a typo, I've corrected it, Thank you very much...
 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok Valentin
I am some what clear with the concept.
Cats and mouse story makes me feel comfortable with the matter.
But still I am not perfectly done with the topic, especially when it comes to <<, because I feel the matter is different here.Am I right?
I would like to ask you, incase you have some site or some link where I can find some examples of shift operators which gives me a better idea(I ned examples and sample questions etc.)then you can just let me know.I think this will make me perfectly clear with the matter.
Thank you,
Sonir
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hate to sound like a broken record, but "write some test programs" using shift operators. The Integer class has the lovely toBinaryString method that will let you print the bit pattern of any int - that makes it really simple to see the binary pattern before and after a shift.
Bill
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sonir,
I agree with William, you should really begin to code the concept you don't get, stick your nose in the API, open your favorite text editor and write some code... Compile, run and look at the output, change the code a bit, compile and run again, look at the different output, etc etc ...
Concerning << I don't see why you feel it is different than >>. The only thing is that bits are shifted to the left and 0's are padded on the right, for instance:
00011001 << 3 yields 11001000. While >> is called signed right shift there is no such thing as a signed left-shift. << just shifts bits to the left while padding 0's on the right...
By left-shifting you may loose the sign information though. Look at the following example:
11111111 is equal to -1 in binary. Now if you left-shift 8 times then you get this:
11111111 << 8 = 00000000. And now you have 0 but if you want to right shift again you are never gonna get 11111111 again because you lost the sign information (the first bit on the left).
But if you left-shift less than 8 times, like this
11111111 << 7 = 10000000 you still have the first bit equal to one (negative number) and you can still right-shift again and get the original number again. 10000000 >> 7 = 11111111
HIH
[ January 20, 2002: Message edited by: Valentin Crettaz ]
 
Hoo hoo hoo! Looks like we got a live one! Here, wave this tiny ad at it:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic