Bit Flipping in Hex

Posted by freyrs on Stack Overflow See other posts from Stack Overflow or by freyrs
Published on 2010-05-31T04:36:50Z Indexed on 2010/05/31 4:42 UTC
Read the original article Hit count: 234

I have an 8 digit hexadecimal number of which I need certain digits to be either 0 or f. Given the specific place of the digits is there a quick way to generate the hex number with those places "flipped" to f. For example:

flip_digits(1) = 0x000000f
flip_digits(1,2,4) = 0x0000f0ff 
flip_digits(1,7,8) = 0xff00000f 

I'm doing this on an embedded device so I can't call any math libraries, I suspect it can be done with just bit shifts but I can't quite figure out the method. Any sort of solution (Python, C, Pseudocode) will work. Thanks in advance.

© Stack Overflow or respective owner

Related posts about language-agnostic

Related posts about bit-manipulation