In-depth understanding of the bitwise XOR operator

XOR operation:

First of all, XOR means that when the binary representation of two numbers is performed XOR operation, if the two binary representations of the current bit are different, it is 1 and the same is 0. This method is widely used to count the number of digits of 1 in a number!

The two values ​​involved in the operation, if the two corresponding bits are the same, the result is 0, otherwise it is 1.
That is:
  0^0 = 0, 
  1^0 = 1, 
  0^1 = 1, 
  1^1 = 0
3 characteristics of bitwise XOR:
(1) 0^0=0,0^1=1 0 XOR or any number = any number
(2) 1^0=1,1^1=0 1 XOR any number - negate any number
(3) any number XOR itself = set itself to 0
bitwise XOR several Common uses:
(1) To flip some specific bits
    such as the 2nd and 3rd bits of the logarithm 10100001, then the number can be XORed with 00000110.
       10100001^00000110 = 10100111

(2) To achieve the exchange of two values ​​without using temporary variables.
    For example, exchanging the values ​​of two integers a=10100001 and b=00000110 can be achieved by the following statements:
    a = a^b; //a=10100111
    b = b^a; //b=10100001
    a = a^b; / /a=00000110

bit operation

In bit operation, after the number is represented in binary, the operation of 0 or 1 on each bit is performed. The first step to understanding bit operations is to understand binary. Binary means that each digit of the number is 0 or 1. For example, 2 in decimal is 10 after being converted into binary.

In fact, binary operations are not difficult to master, because there are only 5 kinds of bit operations: AND, OR, XOR, left shift, right shift. The following table:

 

and(&) 0 & 0 = 0 1 & 0 = 0 0 & 1 = 0 1 & 1 = 1
or (|) 0 | 0 = 0 1 | 0 = 1 0 | 1 = 1 1 | 1 = 1
XOR (^) 0 ^ 0 = 0 1 ^ 0 = 1 0 ^ 1 = 1 1 ^ 1 = 0

Left shift operation :

  The left shift operator m<<n means to shift m to the left by n bits. When shifting n bits to the left, the leftmost n bits will be discarded, and n 0s will be added to the rightmost. For example:

00001010 << 2 = 00101000

10001010 << 3 = 01010000

Right shift operation :

  The right shift operator m>>n means to shift m to the right by n bits. When shifting right by n bits, the rightmost n bits are discarded. But handling the leftmost bit when shifting right is a little more complicated. Note here that if the number is an unsigned value , the leftmost n bits are padded with 0. If the number is a signed numeric value , the leftmost n bits are padded with the sign bit of the number. That is to say, if the number was originally a positive number, then shift to the right and then add n 0s to the left; if the number was originally negative, then shift to the left and then add n 1s to the left. The following is a heap of two 8-bit signed Example of right-shifting a number:

00001010 >> 2 = 00000010

10001010 >> 3 = 11110001

  There is an equivalence relation about shifting operations: shifting an integer to the right by one bit is mathematically equivalent to dividing an integer by 2.

a << = 1 ; // Shifting a to the left is equivalent to a = a * 2;

a <<= 2 ; // Shifting a left by 2 bits is equivalent to a = a * 2 to the power of 2 (4);

   计算机内部只识别1、0,十进制需变成二进制才能使用移位运算符<<,>> 。

复制代码
int j = 8;
p = j << 1;
cout<<p<<endl;

在这里,8左移一位就是8*2的结果16 。

  移位运算是最有效的计算乘/除乘法的运算之一

  按位与(&)其功能是参与运算的两数各对应的二进制位相与。只有对应的两个二进制位均为1时,结果位才为1,否则为0 。参与运算的数以补码方式出现。

先举一个例子如下:

  题目:请实现一个函数,输入一个正数,输出该数二进制表示中1的个数。

复制代码
[cpp]  view plain  copy
 
  1. int count(BYTE n)  
  2. {  
  3.     int num = 0;  
  4.     while(n){  
  5.         n &= (n - 1);  
  6.         num++;  
  7.     }  
  8.     return num;  
  9. }  
复制代码

  这里用到了这样一个知识点:把一个整数减去1,再和原整数做与运算,会把该整数最右边一个1变成0 。 那么一个整数的二进制表示中有多少个1,就可以进行多少次这样的操作。

  总结:把一个整数减去1之后再和原来的整数做位与运算,得到的结果相当于是把整数的二进制表示中的最右边一个1变成0 。

位运算的应用可以运用于很多场合:

  1. 清零特定位(mask中特定位置0,其它位为1 , s = s & mask)。
  2. 取某数中指定位(mask中特定位置,其它位为0, s = s & mask)。

举例:输入两个整数m和n,计算需要改变m的二进制表示中的多少位才能得到n。

解决方法:第一步,求这两个数的异或;第二步,统计异或结果中1的位数。

复制代码
[cpp]  view plain  copy
 
  1. <span style="font-size:18px">#include<iostream>  
  2. using namespace std;  
  3.   
  4. int main()  
  5. {  
  6.     int a = 10 , b =13 , count = 0;  
  7.     int c;  
  8.     c = a ^ b;  
  9.     while(c){  
  10.         c &= (c - 1);  
  11.         count++;  
  12.     }  
  13.     cout<<count<<endl;  
  14.   
  15.     return 0;  
  16. }</span>  


复制代码

 接下来我们再举一例,就可以更好的说明移位运算了:用一条语句判断一个整数是不是2的整数次方。

解决方法:一个整数如果是2的整数次方,那么它的二进制表示中有且只有一位是1,而其它所有位都是0 。 根据前面的分析,把这个整数减去1后再和它自己做与运算,这个整数中唯一的1就变成0了。

解答:!(x & (x - 1))

复制代码

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325849040&siteId=291194637