8 ms·
On a related note, almost any task around shuffling or shifting bits can be improved with xor. For example the straightforward way to remove bit i is mask
by hairtuq 4y ago
On a related note, almost any task around shuffling or shifting bits can be improved with xor. For example the straightforward way to remove bit i is
mask = -1 << i;
return (x & ~mask) | ((x >> 1) & mask);
but with xor we can do it in one less instruction:
mask = -1 << i;
return ((x ^ (x >> 1)) & mask) ^ x;
- deleted 4y ago[deleted]