File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

-4 >> -3

 
Aleksandar Stojanovic
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does shifting with a negative number work?
-4 >> -3
Alex
 
Steve Lovelace
Ranch Hand
Posts: 125
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Aleksandar Stojanovic
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Steve, finaly I've got it.
 
Dimple Kaushik
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 270
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 270
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sagarika ,
You are right.
-3 % 32 = -3

[ November 19, 2003: Message edited by: Cathy Song ]
 
Aleksandar Stojanovic
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 107
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Harwinder Bhatia
Ranch Hand
Posts: 150
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sagarika.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic