This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Shift operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Shift operators" Watch "Shift operators" New topic
Author

Shift operators

Arathi Rajashekar
Ranch Hand

Joined: Nov 20, 2001
Posts: 112
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


Arathi<br />Sun Certified Java Programmer
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
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
Tony reedy
Greenhorn

Joined: Jan 11, 2002
Posts: 21
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

Joined: Nov 20, 2001
Posts: 112
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.
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
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


Rob
SCJP 1.4
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
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

Joined: Jan 07, 2002
Posts: 2205

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Shift operators
 
Similar Threads
negative binary
only for wise
Explain the code...
Two's Complement
Operators