Reputation: 2887
we can shift using >> operator, and we can use '/' to divide in java. What I am asking is what really happens behind the scene when we do these operations, both are exactly same or not..?
Upvotes: 1
Views: 940
Reputation: 2368
Check this out for an explanation on bit shifting: What are bitwise shift (bit-shift) operators and how do they work?
Once you understand that, you should understand the difference between that and the division operation.
Upvotes: 0
Reputation: 133577
No, absolutely not the same.
You can use >>
to divide, yes, but just by 2, because >>
shift all the bits to the right with the consequence of dividing by 2 the number.
This is just because of how binary base operations work. And works for unsigned numbers, for signed ones it depends on which codification are you using and what kind of shift it is.
eg.
122 = 01111010 >> 1 = 00111101 = 61
Upvotes: 3