Isuru Gunawardana
Isuru Gunawardana

Reputation: 2887

What is the difference between '>>' and ' / ' , shifting and division in java

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

Answers (2)

Matt Becker
Matt Becker

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

Jack
Jack

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

Related Questions