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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Bear Bibeault
• Liutauras Vilda
Sheriffs:
• Jeanne Boyarsky
• Junilu Lacar
• Henry Wong
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Jj Roberts
• Tim Holloway
• Piet Souris
Bartenders:
• Himai Minh
• Carey Brown
• salvin francis

# >>> operator

Greenhorn
Posts: 7
I read somewhere that >>> operator always results in a positive number.
Is it statement true or false. I think it is true but answer was given as false. Maybe the exam creator had in mind if a negative number is >>> by 0 then asnwer will be negative?

Greenhorn
Posts: 20
Hi
After the >>> operation if the result is assigned to int variable it results in positive value.But if the result needs to be casted to byte/short it may result in negative value.
correct me if i am wrong.
Regards
jaya s k

Ranch Hand
Posts: 532
hi:
You are right. After the >>> operator x times, the leading x bits is 0s(the left most bits). However, if you cast it to lower number of bits, it might result in a negative number. here is an example:
00000000 00000000 00000011 11111111 // int x = 1023
1- x >>> 2 = 255 // 00000000 00000000 00000000 11111111
2- byte y = (byte) x; // 11111111
In statement 2, we trimmed the int to a byte, which result in a negative number.

Greenhorn
Posts: 8
>>> operator may be -ve example
-1>>>32
in the above case o times we shifted it so the result is remain same as 32/32 we get 0

Ranch Hand
Posts: 125
I believe Manu is onto the point of the question. The shift operators do nothing if the amount to shift is a multiple of 32 in the case of shifting an int, or 64 in the case of a long. This creates the exception to the always-positive rule for >>>.