![](/img/trans.png)
[英]How to return first operand if the second operand of + operator overloading is zero in C++?
[英]how to zero every bit when in second operand is 1?
例:
1010111110110001
0101011100010010
================
1010100010100001
|0 1
-|----
0|0 1
1|0 0
如何在c ++ / c ++ 11中執行此操作?
你可以做一個按位NOT然后然后AND它們: a & ~b
鑒於:
a = 1010111110110001
b = 0101011100010010
然后否定b
給出:
~b = 1010100011101101
做a & ~b
:
a = 1010111110110001
~b = 1010100011101101
-------------------------
a & ~b = 1010100010100001
簡單:
result = op1 & ~op2;
這會按位反轉第二個操作數(1變為0,反之亦然)。 在此之后你使用按位和。 這通常使用位掩碼調用。
您應該能夠按位否定執行按位AND:
result = val1 & ~val2;
你想要第二個操作數的補充的按位AND。
int fun(int x, int y)
{
return x & ~y;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.