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);
同じことをする方法はありますが、それぞれ1回だけ使用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^yy
x & (x ^ y)
x^y
したがって、次のことができます。
int res = x & ~y;