Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# shift operators

Preetha Vasudevan
Ranch Hand
Posts: 33
can anyone explain me how exactly the shift operations << ,>> and >>> work!!

Akhilesh Trivedi
Ranch Hand
Posts: 1608
Hi Preetha!
Check out numbering over here.

http://www.coderanch.com/t/250859/java-programmer-SCJP/certification/Bitwise-Complement-Operator

Then come to shifts,

a << 3

Actually means shift/remove left most 3 bits and add three zeros in the other end.
For ex.
if a = 0000 0101 1101 0011 0000 0000 0000 1101 then
a<<3 would be
0010 1110 1001 1000 0000 00 0001 1010

similarly,
>>4 means remove right most 4 bits and add four sign bit yes sign-bit in the other end. (So +ve numbers will remain +ve and -ve will remain -ve)

>>> is called zero-filled shift. Same as above but fills zeros instead of sign bits. i.e. result will always be +ve. Hope this helps.

[ October 04, 2005: Message edited by: Akhil Trivedi ]
[ October 04, 2005: Message edited by: Akhil Trivedi ]

Preetha Vasudevan
Ranch Hand
Posts: 33
Thanks Akhil

Bert Bates
author
Sheriff
Posts: 8945
17
remember everyone - this topic is on 1.4 but not tiger...

Richard Rex
Ranch Hand
Posts: 100
Hi Akhil,

How about those right operands that are negative numbers. How is this done?

Ex.
a. 8 << -3 = 0
b. 8 >> -3 = 0
c. 8 >>> -3 = 0

Can you explain what is being done for negative numbers on the right operand? Can we move bit negatively?

Ramya Iyer
Ranch Hand
Posts: 66