C++ 位运算Bitwise operations详解 ----- 重要的解题技巧

什么是位运算:

  利用位运算符号进行二进制位计算的操作即为位运算

  维基百科:👉Bitwise operations in C - Wikipedia

六个位运算符:

SymbolOperator
&bitwise AND
|bitwise inclusive OR
^bitwise XOR (exclusive OR)
<<left shift
>>right shift
~bitwise NOT (one's complement) (unary)

& 位与运算符:

&表示AND。使用&进行位二进制操作,是对操作数的每一个二进制位上进行逻辑合取

bit abit ba & b (a AND b)
000
010
100
111

The bitwise AND operator is a single ampersand: . It is just a representation of AND which does its work on the bits of the operands rather than the truth value of the operands. Bitwise binary AND performs logical conjunction (shown in the table above) of the bits in each position of a number in its binary form. &

 例如:

     11001000  
   & 10111000 
     -------- 
   = 10001000

| 位或运算:

| 表示 OR。类似于AND,使用|进行位二进制操作,是对操作数的每一个二进制位上进行逻辑析取

bit abit ba | b (a OR b)
000
011
101
111

Similar to bitwise AND, bitwise OR performs logical disjunction at the bit level. Its result is a 1 if either of the bits is 1 and zero only when both bits are 0. Its symbol is which can be called a pipe. |

例如:

      11001000  
    | 10111000 
      -------- 
    = 11111000

^ 位异或运算:

^表示 XOR,使用|进行位二进制操作,是对操作数的每一个二进制位上进行逻辑异或析取

bit abit ba ^ b (a XOR b)
000
011
101
110

The bitwise XOR (exclusive or) performs an exclusive disjunction, which is equivalent to adding two bits and discarding the carry. The result is zero only when we have two zeroes or two ones.[3] XOR can be used to toggle the bits between 1 and 0. Thus when used in a loop toggles its values between 1 and 0.[4]i = i ^ 1

例如:

      11001000  
    ^ 10111000 
      -------- 
    = 01110000
使用^位运算符交换两个数:
//不用temp交换两个整数
void swap(int& x , int& y)
{
    x ^= y;
    y ^= x;
    x ^= y;
}

>>右移位运算:

>> 需要有两个操作数,在>>符号左边的是被操作数,在>>右边的表示向右移动的位置。

右移位运算符号常用于,÷2操作。

例如:

i = 14; // Bit pattern 00001110 j = i >> 1; // here we have the bit pattern shifted by 1 thus we get 00000111 = 7 which is 14/2

<<左移位运算:

<< 需要有两个操作数,在<<符号左边的是被操作数,在<<右边的表示向左移动的位置。

左移位运算符号常用于,×2操作。

例如:

int i = 7; // Decimal 7 is Binary (2^2) + (2^1) + (2^0) = 0000 0111 int j = 3; // Decimal 3 is Binary (2^1) + (2^0) = 0000 0011 k = (i << j); // Left shift operation multiplies the value by 2 to the power of j in decimal  // Equivalent to adding j zeros to the binary representation of i  // 56 = 7 * 2^3  // 0011 1000 = 0000 0111 << 0000 0011

利用 & 、|、<<完成一个C加法程序
#include <stdio.h>

int main( void )
{
    unsigned int x = 3, y = 1, sum, carry;
    sum = x ^ y; // x XOR y
    carry = x & y; // x AND y
    while (carry != 0)
    {
        carry = carry << 1; // left shift the carry
        x = sum; // initialize x as sum
        y = carry; // initialize y as carry
        sum = x ^ y; // sum is calculated
        carry = x & y; /* carry is calculated, the loop condition is 
                          evaluated and the process is repeated until 
                          carry is equal to 0.
                        */
    }
    printf("%u\n", sum); // the program will print 4
    return 0;
}
 
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值