3 ms·
uint64 insert_zero_bit(uint64 value, int pos) { uint64 top_mask = ~0u64 << pos; return value + (value & top_mask); } > It only works for in
by tromp 2y ago
uint64 insert_zero_bit(uint64 value, int pos) {
uint64 top_mask = ~0u64 << pos;
return value + (value & top_mask);
}
> It only works for inserting exactly 1 bit
But is easily generalized to inserting k bits:
return value + ((1<<k)-1) * (value & top_mask);