• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

-4 >> -3

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How does shifting with a negative number work?
-4 >> -3
Alex
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Steve, finaly I've got it.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See the answer to a similar question here:
https://coderanch.com/t/243849/java-programmer-SCJP/certification/Shift-operations
Cheers
Harwinder
 
Aleksandar Stojanovic
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sagarika.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic