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..?
Asked
Active
Viewed 720 times
1
-
1Is this question suppose to mention division by 2? otherwise, I think the answer should be obvious. – Colin D Jul 17 '12 at 14:31
-
The JIT can optimise a division by a constant power of two to a bit shift. – Peter Lawrey Jul 17 '12 at 14:38
-
@Peter Lawrey: It can not. Shifting a negative number does not always give the same result as dividing does (one some bit patterns it is off by one). – Durandal Jul 17 '12 at 14:43
-
You can do `x >= 0 ? x >> n : (x + (1 << n) -1) >> n;` When n is a constant this is relatively simple. – Peter Lawrey Jul 17 '12 at 14:52
-
This introduces a branch into the instruction flow (? operator). It will depend on the distribution of the data if this will *cost* or *gain* you performance (if the values 'divided' alternate between positive/negative you get a branch misprediction every time..). The JIT doesn't do this on Java6/7, presumably for a good reason. – Durandal Jul 17 '12 at 15:00
2 Answers
2
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

Jack
- 131,802
- 30
- 241
- 343
-
Also as far as I know, compiler optimizes division by numbers that are power of 2 to bitshift operation. – bezmax Jul 17 '12 at 14:35
-
I dont' think Java does it by default, but I should just look at some bytecode to be sure of it. Maybe it gets optimized by JIT but as usually, just guessing doesn't mean anything. – Jack Jul 17 '12 at 14:36
-
1It appears on `int` or `long` : BinaryString("1000") = (int)16 , >> push on position to right and give BinaryString("100")= (int)8, ... – cl-r Jul 17 '12 at 14:37
-
Compilers will *not* replace division by a power of two with shift for *signed* types (its a fairytale). It would generate different results for negative numbers. Try it and you see. – Durandal Jul 17 '12 at 14:41
-
for x=-1024, it works x>>1, x>>2, x>>3,.... And x/2, x/4, x/8,... What I am asking is this computation is done in a similar way, or when dividing it just use division operation and when it do the shifting it get the bit sequence, shift it according to the given input and replace...? @Durandal – Isuru Gunawardana Jul 17 '12 at 17:12
-
2Bitshifting-1 does not result in division by two. (-1>>1) results in -1 whereas (-1/2) results in 0. This has to do with the representation of -1 in binary: 11111111111111111111111111111111 . When you bitshift a negative number a 1 is shifting into the high order bit, leaving the bits exactly the same. – Matt Jul 18 '12 at 02:16
0
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.

Community
- 1
- 1

Matt Becker
- 2,338
- 1
- 28
- 36