C++ 位运算Bitwise operations详解 ----- 重要的解题技巧
什么是位运算:
利用位运算符号进行二进制位计算的操作即为位运算
维基百科:👉Bitwise operations in C - Wikipedia
六个位运算符:
Symbol | Operator |
---|---|
& |
bitwise AND |
| |
bitwise inclusive OR |
^ |
bitwise XOR (exclusive OR) |
<< |
left shift |
>> |
right shift |
~ |
bitwise NOT (one's complement) (unary) |
& 位与运算符:
&表示AND。使用&进行位二进制操作,是对操作数的每一个二进制位上进行逻辑合取
bit a | bit b | a & b (a AND b) |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
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 a | bit b | a | b (a OR b) |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
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 a | bit b | a ^ b (a XOR b) |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
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; }
本文来自博客园,作者:slowlydance2me,转载请注明原文链接:https://www.cnblogs.com/slowlydance2me/p/16888700.html