How can I - in an as efficient manner as possible - create a mask for all nibbles in an unsigned 64-bit integer that match a certain value?
For example, suppose I have a 64-bit unsigned integer:
0000 0100 0011 0011 0011 0011 0010 0010 0010 0010 0010 0010 0001 0001 0001 0001
And say I only want to allow the nibbles that have values of 0010. How can I find those nibbles and create the mask for them. In this contrived example I know of course nibbles 5:10 are 0010, and so the corresponding mask to create is:
0000 0000 0000 0000 0000 0000 1111 1111 1111 1111 1111 1111 0000 0000 0000 0000
But I'd like to create such masks for any 64-bit unsigned integer and any nibble value. I might be interested in 0100
, 0010
, or 1000
nibbles for example.