Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

kumar abhay
Ranch Hand
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.
take care
with regards
kumar abhay

Jim Petersen
Ranch Hand
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

Chicken Farmer ()
Ranch Hand
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
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

Chicken Farmer ()
Ranch Hand
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
Posts: 9080
12

kumar abhay
Ranch Hand
Posts: 53
hi there,
tahnks alot for solving my problem.i will definitely work in detail on bits.
take care
with regards
kumar abhay