• 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: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why does this code give 10 as output

int i=10;
System.out.println("i << 1 = "+(i<<Integer.MIN_VALUE));

since Integer.MIN_VALUE results in a negative value the shift operation should result in 0 as output
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I better show you with an example:

65535 << -28

This is equl to: 1048560


Why?

Well, let�s take the binary representation of -28 and AND it to 31, which is the bigger acceptable number for shifting.

-28: 11111111111111111111111111100100
+31: 00000000000000000000000000011111
AND: 00000000000000000000000000000100

AND = 4

So 65535 << -28 is the same that 65535 << 4

It is the same case when number is positive, but bigger than 31. What I do is to evaluate the difference from number to 32 (maximun number of bits in integers, because just integers can be shifted).

For example 32-28 = 4

Hope it helps!
 
shandilya popuru
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why do we need to and it with 31

form k&b when number is greater than 32 then "thatnumber%32" is used to evaluate the number of times it should be shifted
 
shandilya popuru
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can any one give me a reference to the this topic in the JLS
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are right the shift operator actually does %32 before shifting. Actually Interger.MIN_VALUE%32 is 0 . Thats why you are getting 10 as result.

Hope this helps..

----satish
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Edwin Dalorzo:
I better show you with an example:

65535 << -28

This is equl to: 1048560


Why?

Well, let�s take the binary representation of -28 and AND it to 31, which is the bigger acceptable number for shifting.

-28: 11111111111111111111111111100100
+31: 00000000000000000000000000011111
AND: 00000000000000000000000000000100

AND = 4

So 65535 << -28 is the same that 65535 << 4



Edwin is this concept is right , I mean when we have negative operand right side we will first apply AND operation with this negative number and 31(in case of integer ) & 63(in case of long-I am gussing) and then shift the left hand number by this result .

One more thing , there was some thread about this in that there were some concept of 5 in case on long & 4 in case of int or something like that , That was very good thread . If any body remember then please provide the link .

thanks .
[ February 02, 2005: Message edited by: rathi ji ]
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here you go

Hope that helps
 
shandilya popuru
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cool explanation of the shift in the thread jay, it saves a lot of time while
calculating negative shifts

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic