How zero is each bit when in the second operand is 1?

Example:

1010111110110001
0101011100010010
================
1010100010100001

 |0 1
-|----
0|0 1
1|0 0

how to do this operation in c ++ / c ++ 11?

+5
source share
4 answers

You can do bitwise NOT and then AND: a & ~b

Given:

 a     = 1010111110110001
 b     = 0101011100010010

Then negation bgives:

~b     = 1010100011101101

and execute a & ~b:

 a     = 1010111110110001
~b     = 1010100011101101
-------------------------
a & ~b = 1010100010100001
+7
source

simply:

result = op1 & ~op2;

this inverts the second operand bitwise (1 becomes 0 and vice versa). After that, you use bitwise and. This is often called using a bitmask.

+6
source

You should be able to bitwise AND with bitwise negation:

result = val1 & ~val2;
+6
source

You want a bitwise AND compliment of the second operand.

int fun(int x, int y)
{
    return x & ~y;
}
+4
source

All Articles