C语言教程
C语言控制语句
C语言函数
C语言数组
C语言指针
C语言字符串
C语言数学函数
C语言结构
C语言文件处理
C预处理器

C语言按位运算符

按位运算符是用于对位级别的数据执行运算的运算符。当我们执行按位运算时,这也称为位级编程。它由两位数字(0或1)组成。主要用于数值计算中,以加快计算速度。
在C编程语言中,我们有不同类型的按位运算符。以下是按位运算符的列表:
操作员 操作员的含义
按位与运算符
| 按位或运算符
^ 按位异或运算符
一个补码运算符(一元运算符)
<< 左移运算符
>> 右移运算符
让我们看一下按位运算符的真值表。
X Y X&Y X | Y X ^ Y
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 1

按位AND运算符

按位AND运算符由单个&符号表示。两个整数操作数写在(&)运算符的两侧。如果两个操作数的对应位均为1,则按位与运算的输出为1;否则,为0。否则,输出将为0。
例如,
We have two variables a and b.
a =6;
b=4;
The binary representation of the above two variables are given below:
a = 0110
b = 0100
When we apply the bitwise AND operation in the above two variables, i.e., a&b, the output would be:
Result = 0100
从以上结果可以看出,两个变量的位被一个一个地比较。如果两个变量的位均为1,则输出为1,否则为0。
让我们通过程序了解按位AND运算符。
#include <stdio.h>
int main()
{
   int a=6, b=14;  // variable declarations
   printf("The output of the Bitwise AND operator a&b is %d",a&b);
   return 0;
}
在上面的代码中,我们创建了两个变量,即'a'和'b'。 " a"和" b"的值分别为6和14、 'a'和'b'的二进制值分别是0110和1110、当我们在这两个变量之间应用AND运算符时,
a AND b = 0110 && 1110 = 0110
输出
C中的按位运算符

按位或运算符

按位或运算符由单个垂直符号(|)表示。两个整数操作数写在(|)符号的两侧。如果任何操作数的位值为1,则输出将为1,否则为0。
例如,
We consider two variables,
a = 23;
b = 10;
The binary representation of the above two variables would be:
a = 0001 0111
b = 0000 1010
When we apply the bitwise OR operator in the above two variables, i.e., a|b , then the output would be:
Result = 0001 1111
从上面的结果可以看出,两个操作数的位被一一比较。如果任一位的值为1,则输出为1,否则为0。
让我们通过程序了解按位OR运算符。
#include <stdio.h>
int main()
{
   int a=23,b=10;  // variable declarations
   printf("The output of the Bitwise OR operator a|b is %d",a|b);
   return 0;
}
输出
C

按位异或运算符

按(^)符号表示按位异或运算符。两个操作数写在异或运算符的两侧。如果任何操作数的对应位为1,则输出为1,否则为0。
例如,
We consider two variables a and b,
a = 12;
b = 10;
The binary representation of the above two variables would be:
a = 0000 1100
b = 0000 1010
When we apply the bitwise exclusive OR operator in the above two variables (a^b), then the result would be:
Result = 0000 1110
从以上结果可以看出,两个操作数的位被一一比较;如果任何一个操作数的对应位值为1,则输出为1,否则为0。
让我们通过程序了解按位异或运算符。
#include <stdio.h>
int main()
{
   int a=12,b=10;  // variable declarations
   printf("The output of the Bitwise exclusive OR operator a^b is %d",a^b);
   return 0;
}
输出
C

按位补码运算符

按位补码运算符也称为一个人的补码运算符。它由代字号(〜)表示。它仅接受一个操作数或变量,并对一个操作数执行补码运算。当我们对任意位应用补码运算时,则0变为1,而1变为0。
例如,
if we have a variable named 'a',
a = 8;
The binary representation of the above variable is given below:
a = 1000
When we apply the bitwise complement operator to the operand, then the output would be:
Result = 0111
从上面的结果中我们可以看到,如果该位为1,则它变为0,否则为1、
让我们通过程序来理解补码运算符。
#include <stdio.h>
int main()
{
   int a=8;  // variable declarations
   printf("The output of the Bitwise complement operator ~a is %d",~a);
   return 0;
}
输出
C

按位移位运算符

C编程中存在两种类型的按位移位运算符。按位移位运算符将在左侧或右侧对位进行移位。因此,可以说按位移位算子分为两类:
左移运算符 右移运算符
左移运算符
这是一个将位数移到左侧的运算符。
左移运算符的语法如下:
Operand << n
其中,
操作数是一个整数表达式,我们在其上执行左移运算。
n是要移位的位数。
对于左移运算符,'n'位将在左侧移位。将会弹出左侧的" n"位,右侧的" n"位将填充0。
例如,
Suppose we have a statement:
int a = 5;
The binary representation of 'a' is given below:
a = 0101
if we want to left-shift the above representation by 2, then the statement would be: 
a << 2;
0101<<2 = 00010100
让我们通过一个程序来理解。
#include <stdio.h>
int main()
{
    int a=5; // variable initialization
    printf("The value of a<<2 is : %d ", a<<2);
    return 0;
}
输出
C中的按位运算符
右移运算符
它是将位数向右移位的运算符。
右移运算符的语法如下所示:
Operand >> n;
其中,
操作数是一个整数表达式,我们在其上执行右移运算。
N是要运算的位数
对于右移运算符,'n'位将在右侧移位。将会弹出右侧的'n'位,左侧的'n'位将填充0。
例如
Suppose we have a statement,
int a = 7;
The binary representation of the above variable would be:
a = 0111
if we want to right-shift the above representation by 2, then the statement would be:
a>>2;
0000 0111 >> 2 = 0000 0001
让我们通过一个程序来理解。
#include <stdio.h>
int main()
{
    int a=7; // variable initialization
    printf("The value of a>>2 is : %d ", a>>2);
    return 0;
}
输出
C中的按位运算符
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4