There is a task: There are 2 numbers:
- src specifies numbers from 0 to 31 by setting the corresponding bit.
- dst specifies the positions of the numbers set in src to be selected
For example:
src = 0b11010110
- numbers №1 = 1, №2 = 2, №3 = 4, №4 = 6, №5 = 7
mask = 0b00001010
- extract numbers №1, №3 from src
result = 0b01000100
Can you tell me how quickly you can do this? I wrote the code below, but maybe you can do a lot better?
int unzip_variant(const int src, const int mask)
{
int unzipped = 0;
int pos = 0;
for (int index = 1; index <= с_size; index ++)
{
if (src & (1 << index))
{
pos += 1;
if (mask & (1 << pos))
unzipped |= (1 << index);
}
}
return unzipped;
}