Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does >> and 0xfffffff8 mean?

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!

like image 452
Joel Avatar asked Oct 10 '11 20:10

Joel


People also ask

What is the meaning of >> operator?

Since >> is the binary right-shift operator, it means to shift the value in set right by 1 bit.

What is >> in bitwise?

>> 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 .

What does >> mean in C programming?

Bitwise Right shift operator >> is used to shift the binary sequence to right side by specified position.

What does >> mean in JS?

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.


1 Answers

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.

like image 56
Christian Rau Avatar answered Sep 22 '22 10:09

Christian Rau