I saw something like
somearray[0xFFFF & bi]
in a C /CUDA code, I wonder what does this mean?
CodePudding user response:
In general, the operations x % y and x & (y-1) are equivalent if y is a power of two.
This is the case for your code snipped. Thus,
somearray[0xFFFF & bi] is equivalent to somearray[bi % 65536] which means only the first 65536 array entries will be accessed, but with wrap-around for values of bi which are larger than 65535.
The computation of a bitwise AND is more efficient than plain modulo computation. However, modern compilers should perform this transformation automatically if the righthanded side of modulo is a power of two compile-time constant. I would prefer writing x % 65536 for readability.
CodePudding user response:
The index of the array (somearray) will be calculated as follows:
0xFFFFequals65535decimal, which equals16times binary1s (1111111111111111)- your bi variable will also be 'represented' in binary
- The two will be
ANDed together and you have your index calculated. Ifbiis less then16bit integer, then the index will equal bi, or else, it will be the lesser16bit ofbi- or else, if it is not of typeinteger, you will get compiler error.
CodePudding user response:
This means:
use low 16 bits of bi as somearray 's index.
