• 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

shift operators

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a doubt as follows.

int i = 10;
i = i << 35;

I came to know that it's equivalent to i = i << 3 bcoz 35 % 32 where 32 is the bit depth of int.
Its OK.
But whats the reason to do like that.
If I shift all the bits 32 times I get all 0s then If I shift those all zeros any number of times I will get the same as all zeros.

Can anybody explain me, please...........
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Constitution of java says "The shift distance for int values should be in the range 0 and 31(inclusive)"
hope it helps u!
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ashok reddy devaram:
Constitution of java says "The shift distance for int values should be in the range 0 and 31(inclusive)"
hope it helps u!



Correct. And when it is not, only the lowest 5 bits are used to get it into that range.

Henry
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:


And when it is not, only the lowest 5 bits are used to get it into that range.

Henry



Can you give an example for this? Thank you.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Lets take this example.

int i = 10;
i = i << 35;

here 35 is not in the range of 0 to 31. So we can use lower 5 bits to calculate the shift distance.

35 -- 0000 0000 0000 0000 0000 0000 0010 0011

Here lower 5 bits are -- 00011. This is 3 in decimal.

This means i = i << 35 is equivalant to i = i << 3.

Please correct me if i'm wrong.

Regards,
Surekha.
 
Wendy Lum
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Surekha Reddy:
Hi,

Lets take this example.

int i = 10;
i = i << 35;

here 35 is not in the range of 0 to 31. So we can use lower 5 bits to calculate the shift distance.

35 -- 0000 0000 0000 0000 0000 0000 0010 0011

Here lower 5 bits are -- 00011. This is 3 in decimal.

This means i = i << 35 is equivalant to i = i << 3.

Please correct me if i'm wrong.

Regards,
Surekha.



I think it should be:
i = i << 35; ---> i = i << (35%32); ---> i = i << 3;

There's nothing with the 5 lowest bits. Let me know if I am wrong. Thanks.
 
Surekha Reddy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

What ever u mentioned is also correct.

But the lower 5 bits logic can be used for both positive and negative shift distances.

Regards,
Surekha.
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

There's nothing with the 5 lowest bits.


Maybe I'm misunderstanding you, but the JLS specifies that the five lowest bits are used of the shift operand to determine the shift distance for int values (note that the six lowest bits are used for long values).
 
Wendy Lum
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Steve Morrow:

Maybe I'm misunderstanding you, but the JLS specifies that the five lowest bits are used of the shift operand to determine the shift distance for int values (note that the six lowest bits are used for long values).


I got the idea now. Thank you all!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic