aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes -4 >> -3 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "-4 >> -3" Watch "-4 >> -3" New topic
Author

-4 >> -3

Aleksandar Stojanovic
Greenhorn

Joined: Sep 08, 2003
Posts: 26
How does shifting with a negative number work?
-4 >> -3
Alex


Aleksandar Stojanovic
Steve Lovelace
Ranch Hand

Joined: Sep 03, 2003
Posts: 125
The amount to shift is taken to be the modulus 32 (for int; 64 for long) of the argument, so x >> -3 is taken as x >> 29. By the same token, x >> 35 becomes x >> 3. And watch out for x >> 32; this is a no-op.
Interesting that the vm accomplishes this 'modulus' by simply masking off all but the low 5 bits (6 for long) of the argument, and that this works for negative as well as positive values.
[ November 19, 2003: Message edited by: Steve Lovelace ]

The Inner that is named is not the true Inner.
Aleksandar Stojanovic
Greenhorn

Joined: Sep 08, 2003
Posts: 26
Thanks a lot Steve, finaly I've got it.
Dimple Kaushik
Ranch Hand

Joined: Nov 19, 2003
Posts: 49
Originally posted by Steve Lovelace:
The amount to shift is taken to be the modulus 32 (for int; 64 for long) of the argument, so x >> -3 is taken as x >> 29. By the same token, x >> 35 becomes x >> 3. And watch out for x >> 32; this is a no-op.

steve,
what is x here? both positive and negative ??
Is the rule for all types of shifts??
[ November 19, 2003: Message edited by: dimple kaushik ]
Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270
Hi,
I had a similar question sometime back..
This link might be useful.
[ November 19, 2003: Message edited by: Cathy Song ]
Sagarika nair
Ranch Hand

Joined: Aug 13, 2003
Posts: 39
Hi!!!
It is true that 35%3 is 32.
But how does -3%32 become 29?I don't understand.
-3%32 will be -3 right?
Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270
Hi Sagarika ,
You are right.
-3 % 32 = -3

[ November 19, 2003: Message edited by: Cathy Song ]
Aleksandar Stojanovic
Greenhorn

Joined: Sep 08, 2003
Posts: 26
Wait a minute please .
I would like to ask something.
As we said 4 << 35 = 4 << 3, because 35%32 = 3, am I correct ??? but now lets see problem from the begining.
How should i calculate 4 << -35 ???
Here I've got an example :


public class Test {
public static void main(String[] args){
byte b = (byte)( 7 >> -6 );
System.out.println(b);
}
}
and output from it is 0. ???
Is anyone there who could exmplain me step by step shifting where additiveExmpression is negativ.
Thanks in advance
Sagarika nair
Ranch Hand

Joined: Aug 13, 2003
Posts: 39
Hi!
This is regrading your question about (7>>-6)

The binary representation of 7 is 111
The binary rep of -6 is 1111 1111 1111 1111 1111 1111 1111 1010
The least 5 significant bits of -6 is 11010 which is equal to 26
So 111>>26 =0 b'cos all the ones are shifted out after shifting them just thrice which leaves us with just zeroes even if they are cast to a byte.
I hope I am right friends.
Alan Ford
Ranch Hand

Joined: Oct 26, 2003
Posts: 107
Hi Aco
This is not likely to appear on SCJP 1.4.
If you have K&B book try to follow it.
It is very good. Where are you located.
PM me...


TNT<br />MCP, SCJP 1.4,
Harwinder Bhatia
Ranch Hand

Joined: Oct 17, 2003
Posts: 150
See the answer to a similar question here:
http://www.coderanch.com/t/243849/java-programmer-SCJP/certification/Shift-operations
Cheers
Harwinder
Aleksandar Stojanovic
Greenhorn

Joined: Sep 08, 2003
Posts: 26
Thanks Sagarika.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: -4 >> -3