Just to be pedantic and clarify what was said in the comments, the >>
is an operator bit shift (in this oracle tutorial the separation between operators is clear bitwise - and (&
), or (|
), etc - and operators bit shift).
To be more precise, the language specification (which also puts the operators of bitwise and of shift in separate sections, indicating which technique and pedantly speaking the >>
is not a bitwise Operator and yes a shift Operator) calls that operator "Signed right shift". And as its name says, it moves bits to the right.
Of course if you say you’re a bitwise operator everyone will understand. It’s just my pedantry talking louder...
For example, the number 38 in binary corresponds to 00100110
. If we do 38 >> 4
, means that bits should be moved 4 positions to the right. That is:
00100110 <- valor original (38)
00000010 <- deslocando 4 posições para a direita
Note that the last 4 bits (0110
) are lost with the offset. And on the left, they are filled with zeros. The result is 00000010
, which is equal to number 2 (which is the same result as 38 / 16
).
And why is it equivalent? (with reservations, see more about this at the end)
Forget the binary numbers for now. Imagine the numbers in base 10 (those "normal" ones that everyone uses in day-to-day life).
If I have the number 457090, and I want to divide by 1000 (disregarding the rest of the division and the decimals), all I have to do is eliminate the last 3 digits, resulting in 457.
Basically, if I eliminate only 1 digit of the end (45709), it is the same as dividing by 10. If I eliminate 2 digits at the end (4570) is the same as dividing by 100 (or by 102), if I eliminate 3 digits of the end (457) is the same as divided by 1000 (or by 103) and so on.
In general, if I delete N digits from the end, it is the same as dividing the number by 10N. But that’s only true if the number’s on base 10.
Generalizing this rule, if a number is represented in base B and I delete N digits at the end, the result is the same as dividing that number by BN.
So if the number is in base 2, and I shift 4 positions to the right (which is the same as deleting the last 4 digits), then the result is the same as dividing that number by 24 (that is, the same as dividing by 16).
Whether this will be more optimized or not, the other answer already explained in detail (and the answer is basically, "It depends").
But there’s a catch
All this stuff I’ve been talking about is for positive numbers. If the number is negative (and this is indicated by the first bit, which in negative numbers is 1), then it is no longer equivalent. For example, if the number is -38 we will have the following:
int n = -38;
System.out.println(n / 16); // -2
System.out.println(n >> 4); // -3
That’s because bits are like that now:
11111111111111111111111111011010 <- valor original (-38)
11111111111111111111111111111101 <- deslocar 4 posições para a direita (-3)
When moving to the right, the left positions hold the signal. In the case of positive numbers, the first bit is zero, so when scrolling to the right, the left positions are filled with zero. But in negative numbers the first bit is 1, and the signal is kept while doing the right shift (in fact, that’s why the operator is called "Signed rigth shift" - "Signed" indicates that it is "with signal", that is, the signal is taken into account when making the shift).
The result only equals if the division is exact (for example, if it were -32
, both would result in -2
).
Just out of curiosity, there is also a unsigned right shift (>>>
), that always fills the left positions with zeros (for positive numbers, will continue to be equivalent to the division, but for negatives will give a completely different result).
He calls himself
bitwise operator
, and serves to manipulate the die by inserting bits to the left or right. Practically, if you use a more current compiler, you won’t be able to feel the difference between the two.– ThRnk
@Thrnk Thanks! and can tell me why this is equivalent to a division?
– Braga Us
PS: The operator is not called
bitwise operator
is the name of the category to which that and other operators belong the name isSTR
(Shift to Right). The opposite operator isSTL
(Shift to Left).– Augusto Vasques
How bits are counted by multiplying by 2,
1, 2, 4, 8, 16, 32...
the same would be if it counted backwards, dividing by 2,32, 16, 8, 4...
. For example, we have the value 32 and we want to divide by 2,32 >> 1
, we passed the integer number 1 bit to the right100000
->010000
resulting in 16. It would be something like this.– ThRnk
>> 4
may even be more optimized, but is much less readable and intuitive than/16
when you revisit the code.– nullptr
Basically, if you move the N bits to the right, it is the same as dividing the number by 2 N. Remembering that this is only equivalent to positive numbers (test with a negative and you will see that gives difference). And the language specification calls that operator "Signed right shift"
– hkotsubo