## Have you ever had to use bit shifting in real projects? [closed]

I still write code for systems that do not have floating point support in hardware. In these systems you need bit-shifting for nearly all your arithmetic. Also you need shifts to generate hashes. Polynomial arithmetic (CRC, Reed-Solomon Codes are the mainstream applications) or uses shifts as well. However, shifts are just used because they are … Read more

## Fastest way to set __m256 value to all ONE bits

See also Set all bits in CPU register to 1 efficiently which covers AVX, AVX2, and AVX512 zmm and k (mask) registers. You obviously didn’t even look at the asm output, which is trivial to do: #include <immintrin.h> __m256i all_ones(void) { return _mm256_set1_epi64x(-1); } compiles to with GCC and clang with any -march that includes … Read more

## The difference between logical shift right, arithmetic shift right, and rotate right

First remember that machine words are of fixed size. Say 4, and that your input is: +—+—+—+—+ | a | b | c | d | +—+—+—+—+ Then pushing everything one position to the left gives: +—+—+—+—+ | b | c | d | X | +—+—+—+—+ Question what to put as X? with a … Read more

## How to de-interleave bits (UnMortonizing?)

Given that you know that every other bit is 0 in your application, you can do it like this: x = (x | (x >> 1)) & 0x33333333; x = (x | (x >> 2)) & 0x0f0f0f0f; x = (x | (x >> 4)) & 0x00ff00ff; x = (x | (x >> 8)) & 0x0000ffff; … Read more

## Implement division with bit-wise operator

The standard way to do division is by implementing binary long-division. This involves subtraction, so as long as you don’t discount this as not a bit-wise operation, then this is what you should do. (Note that you can of course implement subtraction, very tediously, using bitwise logical operations.) In essence, if you’re doing Q = … Read more