一、按位与&
定义:按位与运算符“&”是双目运算符。其功能是参与运算的两数各对应的二进位相与。操作数都必须是整型的。
规则:0&0=0; 0&1=0; 1&0=0; 1&1=1。
main()
{
int a,b; a=077;
b=a&3;
printf("\40: The a & b(decimal) is %d \n",b);
b&=7;
printf("\40: The a & b(decimal) is %d \n",b);
}
打印输出:
: The a & b(decimal) is 3
: The a & b(decimal) is 3
二、按位或 |
定义:按位或运算符“|”是双目运算符。其功能是参与运算的两数各对应的二进位相或。当参与运算的是负数时,参与两个数均以补码出现。
规则:0|0=0; 0|1=1; 1|0=1; 1|1=1。
main()
{
int a,b;
a=077;
b=a|3;
printf("\40: The a & b(decimal) is %d \n",b);
b|=7;
printf("\40: The a & b(decimal) is %d \n",b);
}
打印输出:
: The a & b(decimal) is 63
: The a & b(decimal) is 63
三、按位异或
定义:参加运算的两个数据,按二进制位进行“异或”运算。
规则:0^0=0;0^1=1;1^0=1;1^1=0。
void main()
{
int a,b;
a=077;
b=a^3;
printf("\40: The a & b(decimal) is %d \n",b);
b^=7;
printf("\40: The a & b(decimal) is %d \n",b);
}
打印输出:
: The a & b(decimal) is 60
: The a & b(decimal) is 59
不同长度的数据进行位运算
如果两个不同长度的数据进行位运算时,系统会将二者按右端对齐,然后进行位运算。
整理转载:https://www.jianshu.com/p/c70af2c9af79,这篇文章更为全面详细。
收!