c++ binary left shift fast front i position 0
template<class T>
static T CreateMask(uint32_t bits) {
return (1 << (sizeof(T) * 8 - bits)) - 1;
}
1 is written in binary as 0b00000001
Assumption: shift 1 to the left by 2 bits to get 0b00000100 , subtract one to get 0b00000011
, just set the last 2 bits to 1
Therefore, the i bit after the left shift is 1, and the former sizeof(T)*8-i bit is 0.
So if you want to obtain a binary mask with the first i bit being 0, you need to shift 1 to the left by sizeof(T)*8 - i.
c++ binary left fast front i position 1
On the basis of the previous i position 0, just invert ~