Suppose we have a byte B
and a mask M
such that one of the bits in the mask is either 1 or 0 and the others are 0, (suppose we know which bit that is, it can be any bit but for simplicity it's the LSB). We want that the same bit in B
will have the same value as that bit from M
.
For example if B=1111
, M=0000
then we want to have after the operation B=1110
, but if M
was M=0001
then B
would have been B=1111
.
I'm basically asking how to implement the boolean function generated from this truth table:
old new result
0 0 0
0 1 1
1 0 0
1 1 1
So result
would be:
a=old, b= new
result=a'b+ab
Doing B = ~B&M | B&M;
in C doesn't seem to work...
I probably can do it with conditionals but there's got to be a way to it without them.
Another example:
If we care about the second bit, and if B=0001
, M=0010
then the result of the operation would be 0011
. But if M
was M=0000
then the result of the operation was 0001
.
I know how to set clear and toggle a bit, this is not what I'm asking.