Bit-wise Operation and Logical Operation of C language

Bit-Wise Operations (bit operations) 
include:
& bitwise and
| bitwise or
^ bitwise exclusive or
~ negation
<< left shift
>> right shift

Some intuitive:
1. Bitwise AND

The bitwise AND operator "&" is a binary operator. Its function is the binary phase AND of the corresponding two numbers involved in the operation. The result bit is 1 only if the corresponding two binary bits are both 1, otherwise it is 0. The numbers involved in the operation appear in two's complement form.

 

For example: 9&5 can be written as follows: 00001001 (2's complement of 9)&00000101 (2's complement of 5) 00000001 (2's complement of 1) shows that 9&5=1.

 

  The bitwise AND operation is usually used to clear certain bits or reserve certain bits. For example, clear the high eight bits of a to 0, and keep the low eight bits, which can be used for a&255 operation (the binary number of 255 is 0000000011111111).

main(){
int a=9,b=5,c;
c=a&b;
printf("a=%d/nb=%d/nc=%d/n",a,b,c);
}

 

 

2. Bitwise OR
 

The bitwise OR operator "|" is a binary operator. Its function is the binary phase OR of the corresponding two numbers involved in the operation. As long as one of the corresponding two binary bits is 1, the result bit is 1. Both numbers involved in the operation appear in complement.
For example: 9|5 can be written as follows: 00001001|00000101
00001101 (13 in decimal), 9|5=13

main(){
int a=9,b=5,c;
c=a|b;
printf("a=%d/nb=%d/nc=%d/n",a,b,c);
}

 

 

 

3. Bitwise XOR

 

The bitwise XOR operator "^" is a binary operator. Its function is that the corresponding binary bits of the two numbers involved in the operation are XORed. When the two corresponding binary bits are different, the result is 1. Participating operands still appear in complement, for example, 9^5 can be written as the following formula: 00001001^00000101 00001100 (12 in decimal)

main(){
int a=9;
a=a^15;
printf("a=%d/n",a);
}

 

 

 

4. Negation

 

The negation operator ~ is a unary operator with right associativity. Its function is to invert each binary bit of the number involved in the operation. For example, the operation of ~9 is: ~(0000000000001001) The result is: 1111111111110110

 

5. Left shift operation

 

左移运算符“<<”是双目运算符。其功能把“<< ”左边的运算数的各二进位全部左移若干位,由“<<”右边的数指定移动的位数,
高位丢弃,低位补0。例如: a<<4 指把a的各二进位向左移动4位。如a=00000011(十进制3),左移4位后为00110000(十进制48)。

 

6. 右移运算

 

右移运算符“>>”是双目运算符。其功能是把“>> ”左边的运算数的各二进位全部右移若干位,“>>”右边的数指定移动的位数。
例如:设 a=15,a>>2 表示把000001111右移为00000011(十进制3)。 应该说明的是,对于有符号数,在右移时,符号位将随同移动。当为正数时, 最高位补0,而为负数时,符号位为1,最高位是补0或是补1 取决于编译系统的规定。Turbo C和很多系统规定为补1。

main(){
unsigned a,b;
printf("input a number: ");
scanf("%d",&a);
b=a>>5;
b=b&15;
printf("a=%d/tb=%d/n",a,b);
}


请再看一例!

main(){
char a='a',b='b';
int p,c,d;
p=a;
p=(p<<8)|b;
d=p&0xff;
c=(p&0xff00)>>8;
printf("a=%d/nb=%d/nc=%d/nd=%d/n",a,b,c,d);
}

 

 

Logical Operations (逻辑运算)
&&:逻辑与,前后条件同时满足表达式为真 ||:逻辑或,前后条件只要有一个满足表达式为真 ! :逻辑非

例子:
!0x41 = 0x00
!0x00 = 0x01
!!0x41 = 0x01
0x69&&0x55 = 0x01
0x69||0x55 = 0x01

注意 : 当逻辑运算第一部分已经满足条件时,第二部分不会执行。
参考来源:
https://blog.csdn.net/xing_hao/article/details/6677809


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325273474&siteId=291194637