Small cookie logical operation

I got some ideas from the planning group and understood that logical operations are actually based on two's complement as the carrier. Applying
binary thinking to understand, values ​​exist in the memory in the form of two's complement.
The complement of a positive number is the original code, and the complement of a negative number is: the sign bit remains unchanged, the original code is inverted and plus one.

& (bitwise AND) - all 1s are 1, otherwise 0
| (bitwise OR) - all 1s are 1, otherwise 0
^ (bitwise exclusive OR) - the same is 0, different is 1.
Note a virtually unchanged

#include<stdio.h>
int main()
{
    
      int a = 10;
 int b = a << 2;
 printf("a = %d\nb = %d\n", a, b);
 

 //     00000000000000000000000000001010--a在内存中的二进制
 // (00)00000000000000000000000000101000--a左移一位产生的结果,
 //左移出去的0抛弃
 int c=1,d=2;
 printf("%d %d",c&d,c|d);
 //00000001
 //00000010
 //00000000 a&b =0
 //00000011 a|b =3
}

Guess you like

Origin blog.csdn.net/weixin_50925658/article/details/120599517