binary - Convert bit sequence to uint32_t in c++ -
user specifies register (lfsr) length integer parameter function, example enters number 5. need initialize 5-bit length lfsr 1 bits (for length 5 11111
) , seed mask in format uint32_t
- 5-length register 0x0001f
.
what best way mask 0x0001f
5 bit length register when user enters length of register integer number 5?
to generate mask of n bits (where n < 32):
uint32_t mask = (1u << n) - 1u;
explanation: consider example n = 5:
1u << n = 1u << 5 = 0000 0000 0000 0000 0000 0000 0010 0000 = 0x20
then subtract 1 , get:
0000 0000 0000 0000 0000 0000 0001 1111 = 0x1f
Comments
Post a Comment