Bitfield mask/operations with optional items

Posted by user1560249 on Stack Overflow See other posts from Stack Overflow or by user1560249
Published on 2014-08-22T21:09:26Z Indexed on 2014/08/22 22:20 UTC
Read the original article Hit count: 218

I'm trying to find a way to handle several bitfield cases that include optional, required, and not allowed positions.

yy?nnn?y
11000001

?yyy?nnn
01110000

nn?yyy?n
00011100

?nnn?yyy
00000111

In these four cases, the ? indicates that the bit can be either 1 or 0 while y indicates a 1 is required and n indicates that a 0 is required. The bits to the left/right of the required bits can be anything and the remaining bits must be 0. Is there a masking method I can use to test if an input bit set satisfies one of these cases?

© Stack Overflow or respective owner

Related posts about bit-manipulation

Related posts about bit-fields