• 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: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I want to know how does
4<<-5
results in 1342177280.
I read from previous posting that
If the lefthand operand is of type long (64 bits) then only the last 6 bits of the right hand side are used. If the lefhand operand is an int (32 bits) then only the last 5 bits of the right hand operator are used.
The answer will be right if we won't take two's complement of negative number i.t 5
We are suppose to take two compliement of negative number right. Do any one clear my doubt
Say 4<<-5 without two's complement to represent -ve number
00000000 00000000 00000000 00000100<<11111111
when we take 5 bits of right hand side
00000000 00000000 00000000 00000100<<11100
4<<28
this results in above value

but if we take two's comlement of -ve number
00000000 00000000 00000000 00000100<<11111111 11111111 11111111 11111011
00000000 00000000 00000000 00000100<<11011
4<<27
this results in different value other than above.
Here i am confused. Do we have to take -ve complement or not
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I read from previous posting that
If the lefthand operand is of type long (64 bits) then only the last 6 bits of the right hand side are used. If the lefhand operand is an int (32 bits) then only the last 5 bits of the right hand operator are used.


If the left hand operator is an int then only the last 4 bytes NOT Bits of the right hand operator are used. ( the same length as an int )
HTH
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Arthi,
Your logic of 5 bits with 2's compliment is working fine for me.

just try out this simple code.

both are giving the same number.
I hope this helps
-Tony.
 
Arathi Rajashekar
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tony reedy:
Hi Arthi,
Your logic of 5 bits with 2's compliment is working fine for me.

just try out this simple code.

both are giving the same number.
I hope this helps
-Tony.


Hi Tony.
It worked fine for me. But will you compile and will you clarify this for me again.
Change in the above code 4<<-5 to
16<<-12.
I am getting different answer when I calculte manually and when I run the program.
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shivaji Marathe:

If the left hand operator is an int then only the last 4 bytes NOT Bits of the right hand operator are used. ( the same length as an int )
HTH


Actually, this is wrong.
JLS 15.19


If the promoted type of the left-hand operand is an int, only the five lowest-order bits of the right-hand operand are used as the shift distance....
If the promoted type of the left-hand operand is a long, only the six lowest-order bits of the right-hand operand are used as the shift distance....

Rob

 
Shivaji Marathe
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OOPS. Of course Rob is right . The low order 5 bits can define any number from 0 to 31. And that is the range of numbers by which an int can be shifted. In other words, the right hand operator always has a value between 0 and 31.
By the same logic a long can be shifted aby any number between 0 and 63.
I don't know what I was smoking when I posted earlier
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I don't know what I was smoking when I posted earlier



I don't either, but make sure you save it for AFTER your SCJP test!!
(and save me some!)


Rob :roll:
[ January 15, 2002: Message edited by: Rob Ross ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic