c语言 ++ --运算符_C / C ++中的按位运算符

c语言 ++ --运算符

In any programming language, operators carry out manipulations and operations on the operands, variables, etc. In C language or C++ language, Bitwise operators work at the bit level and serve the purpose of manipulations and operations on the Bits.

在任何编程语言中,运算符都对操作数,变量等进行操作和运算。在C语言C ++语言中 ,按位运算符在位级别上工作,并且用于对位进行运算和操作。

Process:

处理:

  • Bitwise operators perform operations on integers at a bit level.

    按位运算符对整数进行位级运算。
  • The input (integer) gets converted to binary form after which the operations on the binary value are performed.

    输入(整数)将转换为二进制形式,然后对二进制值执行运算。
  • After the manipulations, the binary value obtained gets converted back to the integer format and gets displayed as a result.

    操作后,获得的二进制值将转换回整数格式并作为结果显示。


C和C ++中按位运算符的类型 (Types of Bitwise Operators in C and C++)

OperatorFunction
Bitwise AND Operator (&)The operator returns 1, if both the operand’s bits are 1.
Bitwise OR Operator (|)The operator returns 1, if either of the operand’s bits are 1.
Bitwise XOR Operator (^)The operator returns 1, if both the operand’s bits are opposite to each other, else it returns 0.
Bitwise 1’s Complement Operator (~)This operator inverts all the bits of the operand.
Bitwise Left Shift Operator (<<)This operator shifts the bits to left by an offset provided by the user.
Bitwise Right Shift Operator (>>)This operator shifts the bits to right by an offset provided by the user.
操作员 功能
按位AND运算符(&) 如果两个操作数的位均为1,则运算符返回1。
按位或运算符(|) 如果操作数的任一位为1,则运算符返回1。
按位XOR运算符(^) 如果两个操作数的位彼此相反,则运算符返回1,否则返回0。
按位1的补码运算符(〜) 该运算符将操作数的所有位取反。
按位左移运算符(<<) 该运算符将位向左移动用户提供的偏移量。
按位右移运算符(>>) 该运算符将位右移用户提供的偏移量。


1.按位与运算符 (1. Bitwise AND Operator)

The operator results 1 as output if both the bits are 1, else it results to 0.

操作结果1作为输出,如果两个位都为1,否则它会导致为0。

Syntax:

句法:


operand1 & operand2

Example:

例:

5 (Decimal) = 00000101 (Binary)

5(十进制)= 00000101(二进制)

4 (Decimal) = 00000100

4(十进制)= 00000100

5 & 4 = 00000101 & 00000100 = 00000100 = 4

5&4 = 00000101&00000100 = 00000100 = 4

Result: 5 & 4 = 4

结果:5&4 = 4


#include <stdio.h>
int main()
{
    int num1 = 5, num2 = 4;
    int result = num1&num2;
    printf("num1 & num2 = %d", result);
    return 0;
}

Output:

输出:


num1 & num2 = 4


2.按位或运算符 (2. Bitwise OR Operator)

The operator results 1 as output if any of the bits are 1, else it results to 0.

操作结果1作为输出,如果任何位是1,否则它会导致为0。

Syntax:

句法:


operand1 | operand2

Example:

例:

5 (Decimal) = 00000101 (Binary)

5(十进制)= 00000101(二进制)

4 (Decimal) = 00000100 (Binary)

4(十进制)= 00000100(二进制)

5 | 4 = 00000101 | 00000100 = 00000101 = 5

5 | 4 = 00000101 | 00000100 = 00000101 = 5

Result: 5 | 4 = 00000101 = 5

结果:5 | 4 = 00000101 = 5


#include <stdio.h>
int main()
{
    int num1 = 5, num2 = 4;
    int result = num1|num2;
    printf("num1 | num2 = %d", result);
    return 0;
}

Output:

输出:


num1 | num2 = 5


3.按位XOR运算符 (3. Bitwise XOR Operator)

The operator results 1 as output if both the bits are different/opposite to each other, else it results to 0.

如果两个位互不相同/相反,则运算符的结果为1 ,否则为0。

Syntax:

句法:


operand1 ^ operand2

Example:

例:

5 (Decimal) = 00000101 (Binary)

5(十进制)= 00000101(二进制)

4 (Decimal) = 00000100 (Binary)

4(十进制)= 00000100(二进制)

5 ^ 4 = 00000101 ^ 00000100 = 00000001 = 1

5 ^ 4 = 00000101 ^ 00000100 = 00000001 = 1

Result: 5 ^ 4 = 00000001 = 1

结果:5 ^ 4 = 00000001 = 1


#include <stdio.h>
int main()
{
    int num1 = 5, num2 = 4;
    int result = num1^num2;
    printf("num1 ^ num2 = %d", result);
    return 0;
}

Output:

输出:


num1 ^ num2 = 1


4.按位左移运算符 (4. Bitwise Left Shift Operator)

This operator shifts the bits of the input to the left by the provided offset value and fill the void spaces as 0 in the result.

该运算符将输入的位向左移动提供的偏移值,并在结果中将空白填充为0。

It is equivalent to multiplying a number by pow(2, offset) i.e. multiplying by 2 raised to the power of offset.

等效于将一个数乘以pow(2,offset),即乘以2可以得到offset的幂。

Syntax:

句法:


operand << offset

Example:

例:

5 (Decimal) = 00000101 (Binary)

5(十进制)= 00000101(二进制)

5 << 2 = 20

5 << 2 = 20


#include <stdio.h>
int main()
{
    int num = 5;
    int result = num << 2;
    printf("num << 2 = %d", result);
    return 0;
}

Output:

输出:


num << 2 = 20


5.按位右移运算符 (5. Bitwise Right Shift Operator)

This operator shifts the bits of the input to the right by the provided offset value and fills the void spaces as 0 in the result.

该运算符将输入的位向右移动所提供的偏移值,并在结果中将空白填充为0。

It is equivalent to dividing a number by pow(2, offset) i.e. dividing by 2 raised to the power of offset.

等效于将数字除以pow(2,offset),即除以2会得到偏移的幂。

Syntax:

句法:


operand >> offset

Example:

例:

5 (Decimal) = 00000101 (Binary)

5(十进制)= 00000101(二进制)

5 >> 1 = 2

5 >> 1 = 2


#include <stdio.h>
int main()
{
    int num = 5;
    int result = num >> 1;
    printf("num >> 1 = %d", result);
    return 0;
}

Output:

输出:


num >> 1 = 2


6.按位1的补码运算符 (6. Bitwise 1’s Complement Operator)

This operator performs 1’s complement on the input. It inverts the bits of the input.

该运算符对输入执行1的补码。 它将输入的位反转。

Bitwise complement of input integer (x) will be – (x+1).

输入整数(x)的按位补码将为–(x + 1)。

Syntax:

句法:


~(operand)

Example:

例:

input = 5 (Decimal) = 00000101 (Binary)

输入= 5(十进制)= 00000101(二进制)

~ input = ~ (00000101) = – (000000101 + 1) = – ( 00000110) = – 6

〜输入=〜(00000101)= –(000000101 +1)= –(00000110)= – 6


#include <stdio.h>
int main()
{
    int num = 5;
    int result = ~(num);
    printf("~num = %d", result);
    return 0;
}

Output:

输出:


~num = -6


结论 (Conclusion)

Thus, in this article, we have understood the basic functionality served by the Bitwise Operators of C/C++.

因此,在本文中,我们已经了解了C / C ++的按位运算符提供的基本功能。



参考资料 (References)

翻译自: https://www.journaldev.com/34816/bitwise-operators-in-c-c-plus-plus

c语言 ++ --运算符

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值