Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有以下 C 语句:
int res = x & (x ^ y);
有没有办法做同样的事情,但每次只使用x一次y?
x
y
例如:
x | (~x & y) == x | y
是的,通过扩展 xor ( a ^ b == (a & ~b) | (~a & b)),然后简化结果,可以得到:
a ^ b == (a & ~b) | (~a & b)
res = x & ~y;
x & (x ^ y)设置已设置x和设置的位,x^y例如未设置的位y。
x & (x ^ y)
x^y
所以你可以这样做:
int res = x & ~y;