按位或0

Mys*_*Dev 1 c bit-manipulation

按位运算符非常有趣所以我试图找出一种方法来产生相反的效果,或者不是xor的效果.

如果我有两个字符(8位):

char1    char2
=====    =====
1010     1000

after transformation

1000     1000
Run Code Online (Sandbox Code Playgroud)

如果0010是我要将char1和char2转移到该注册商处禁用的值(位置1)

我该怎么办?

or如果为1和1,则仅设置1,并且xor仅在1和0或0和1时交换值

Rei*_*ica 7

的相对的和不.您可以使用设置位,而 不是清除位.

#include <assert.h>

int main() {
    int a = 16, b, c;

    // Set bit 3
    b = a | 1<<3; // OR
    assert(b == (8+16));


    // Clear bit 3
    c = b & ~(1<<3); // AND-NOT
    assert(c == 16);
}
Run Code Online (Sandbox Code Playgroud)