aspose file tools*
The moose likes Beginning Java and the fly likes asking about  difference between >>&>>> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "asking about  difference between >>&>>>" Watch "asking about  difference between >>&>>>" New topic
Author

asking about difference between >>&>>>

kumar abhay
Ranch Hand

Joined: Oct 02, 2001
Posts: 53
hi there,
hope u ppl r doing fine.i have a problem regarding right shift operator given below:
question: (i) short a=80>>2;
System.out.println(a);
output is:20
ii) short a=80>>>2;
System.out.println(a);
output is again:20
my question is :
1. what is the basic difference between these 2 operators,because it seems taht apperently there is no difference i see because of same output being produced as 20 in both cases,
so please tell me the difference so that i could move ahead in the shift operators.
2.why this is with right shift operator only it could have been with the left shift operator. i hope that you will help me out of it.
thanks in advance.
take care
with regards
kumar abhay
Jim Petersen
Ranch Hand

Joined: Jul 24, 2001
Posts: 241
Hi Kumar
try experimenting with >>> and >> on a negative number
x >> n (RH Shift-with-sign ('signed shift')) Shifts x bits n times filling with the sign bit from the left (ie x were a negative number there will be 1 in the sign bit so 1s will be filled in)
x >>> n (RH with 0 fill ('unsigned shift')) Shifts the x bits n times filling but fills the sign bit with 0 regardless if the number is negative or positive
eg byte b = -42 // 11010110
int result = b >>> 4 // 268435453
b >>> 4
= 1111 1111 1111 1111 1111 1111 1101 0110 >>> 4 // nb automatic promotion from byte to int (32 bits)
= 0000 1111 1111 1111 1111 1111 1111 1101
= 268435453
hope this helps! - Jim


- Jim Petersen <br />SCJP2<br />SCWCD<p>- but then again, I could be wrong...
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
You're right, in the situation above, it works the same, but try this:

Quite a bit of difference there, eh?
kumar abhay
Ranch Hand

Joined: Oct 02, 2001
Posts: 53
hi there,
thanks alot but i have a follow up question n i hope that this time too you would help me out,given below:
int a=-1>>>1;
System.out.println(a);
output is 2147483647.(why)
By placing minus(-) to the fourth(iv) statement,
the output changed from 1 to 2147483647.could you please explain this to me.i will wait for your reply.
take care
with regards
kumar abhay
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Best way to learn the bit stuff, and this is just coming from my TCP/IP subnetting days, is to actually sit down and write out what the bits are doing. Once you have done this a few times, you will start to realize why certain things are happening, and how to extract values from it.
Biggest thing to remember is >>> always adds 0's to the end, where as >> just replicates whatever bit, a 1 or 0, is at the end.
Also, if you are not familiar with how negative numbers are achieved, search through some of the posts, or the web, for 2's compliment. The >>, >>> , and << operators are a lot easier to understand once you grasp binary.
Jason
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
This page about bits may also be helpful.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
kumar abhay
Ranch Hand

Joined: Oct 02, 2001
Posts: 53
hi there,
tahnks alot for solving my problem.i will definitely work in detail on bits.
take care
with regards
kumar abhay
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Always forget about the Cat and Mouse! Yes, read that page, it was a tremendous help.
Jason
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: asking about difference between >>&>>>