I was told that (i >> 3) is faster than (i/8)
but I can't find any information on what >>
is. Can anyone point me to a link that explains it?
The same person told me "int k = i/8
, followed by k*8
is better accomplished by (i&0xfffffff8);
" but again Google didn't help m...
Thanks for any links!
Since >> is the binary right-shift operator, it means to shift the value in set right by 1 bit.
>> Indicates the bits are to be shifted to the right. Each operand must have an integral or enumeration type. The compiler performs integral promotions on the operands, and then the right operand is converted to type int .
Bitwise Right shift operator >> is used to shift the binary sequence to right side by specified position.
The right shift operator ( >> ) returns the signed number represented by the result of performing a sign-extending shift of the binary representation of the first operand (evaluated as a two's complement bit string) to the right by the number of bits, modulo 32, specified in the second operand.
As explained here the >>
operator is simply a bitwise shift of the bits of i
. So shifting i
1 bit to the right results in an integer-division by 2 and shifting by 3 bits results in a division by 2^3=8.
But nowadays this optimization for division by a power of two should not really be done anymore, as compilers should be smart enough to do this themselves.
Similarly a bitwise AND with 0xFFFFFFF8
(1...1000, last 3 bits 0) is equal to rounding down i
to the nearest multiple of 8 (like (i/8)*8
does), as it will zero the last 3 bits of i
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With