Bit operation-efficient operation skills

Basic operation of bit operation:

  • & (And): both are 1 and the result is 1
  • | (Or): If one is 1, the result is 1
  • ~ (Not): 0 becomes 1, 1 becomes 0
  • ^ (Exclusive OR): only 1 when the two are not the same
  • (>>): Right shift operator, shift the binary bit to the right, shifting one bit to the right is equivalent to dividing by 2
  • (<<): Left shift operator, shift the binary digits to the left, shifting one bit to the left is equivalent to multiplying by 2
  • (>>): The sign bit will be used to fill the high bits
  • For the int type, 1<<35 and 1<<3 are the same (because the int32 bit needs to be modulo 32), and when the left operand is of the long type, it needs to be modulo 64

Some properties of XOR:

  • Commutative law, the position of the factor can be exchanged at will, the result remains
  • Associative law, that is (a^ b) ^ c == a ^ (b ^ c)
  • For any number x, there is x^x=0, x ^0=x, the same as oneself or equal to 0, and the same as zero for oneself
  • Reflexivity, A ^ B ^ B=A ^ 0 = A, XOR operation with the same factor continuously, the final result is itself

Some tips for using bit operations:

1. Determine the odd and even number

For odd numbers, the last digit of the binary must be 1, and for even numbers, the last digit of the binary must be 0, so it is enough to judge whether a number is odd or even and directly AND with 1

#include <iostream>
using namespace std;
int main()
{
    
    
	int num;
	cin>>num;
	if(num&1)
		cout<<num<<"为奇数"<<endl;
	else
		cout<<num<<"为偶数"<<endl; 
		
	return 0;
 } 

2. Exchange the values ​​of two integer variables

Given two integers A and B, it is required to exchange the values ​​of the two numbers. The conventional method is to define another variable and then exchange. The most efficient method is still bit operation.
A=A ^ B
B=A ^ B (At this time B=(A ^ B)^B = A)
A=A ^ B (At this time A= (A ^ B)^A = B)
Thus, it is completed The exchange of A and B

#include <iostream>
using namespace std;
int main()
{
    
    
int a,b;
	cin>>a>>b;
	a=a^b;
	b=a^b;
	a=a^b;
	cout<<"a="<<a<<endl;
	cout<<"b="<<b<<endl;	
			
	return 0;
 } 

3. Get whether the binary bit is 1 or 0

For example, given a number a, it is required to find whether the x-th bit of the binary number is 1 or 0.
At this time, the k-th bit of the number and 1 can be directly ANDed, that is, 1 is shifted to the left by k-1 bits, and then Perform an AND operation with the number. If it is 1, it means that the xth bit is 1, and if it is 0, it means that the xth bit is 0.
num&(1<<k-1)

#include <iostream>
using namespace std;
int main()
{
    
    
int num,k;//k表示num的第k位 
	cin>>num>>k;
	if(num&(1<<k-1))
		cout<<num<<"的第"<<k<<"位为:1"<<endl;
	else
		cout<<num<<"的第"<<k<<"位为:0"<<endl;
			
	return 0;
 } 

4. Find the absolute value of an integer

How to get the absolute value of a number without using the judgment statement, and at this time, the bit operation is used.
1. For a positive number, its sign bit is 0, so the positive number is shifted to the right (signed <<) 31 Bit, will get all 0s, and the value is 0 at this time. The absolute value of a positive number is itself
2. For a negative number, its sign bit is 1. Therefore, if the negative number is shifted right by 31 bits (signed <<), all 1s will be obtained, and the value is -1 (because The computer is stored in one's complement). The absolute value of a negative number can be obtained by inverting it and adding one.

int t=(a>>31);
如果a是正数,则t为0,a为负数,则t为-1
int ans=(a^t)-t;
a为正数,绝对值就是其本身
a为负数时,a^t就是对a进行按位取反,t为-1,
减去t就是加上1,从而得到其绝对值。
#include <iostream>
using namespace std;
int main()
{
    
    
	int a;
	cin>>a;
	int t=(a>>31);
	int ans=(a^t)-t;
	cout<<ans<<endl; 
			
	return 0;
 } 

5. Find the number of 1 in the number

There are two ways to find the number of 1s in the binary system of a number

(1) The int type number is 32 bits, so the for loop 32 times, the number and 1 are left shifted by k bits, and the result obtained is 1 left by k bits equal, then the kth bit of the number is 1. .

#include <iostream>
using namespace std;
int main()
{
    
    
int n;
	cin>>n;
	int cnt=0;
	for(int i=0;i<32;i++)
	{
    
    
		if((n&(1<<i))==(1<<i))
			cnt++;
	}
	cout<<cnt<<endl;
			
	return 0;
 } 

(2) Assuming that the binary of this number is 10100, first subtract the number by one to become 10011. (It can be found that as long as the number is subtracted by 1, the lowest bit of the number becomes 0, and the following bit changes to 1) , The AND operation of 10011 and 10100 is 10000, and a 1 is eliminated, so the number of eliminated 1s can be recorded until the number is 0.

#include <iostream>
using namespace std;
int main()
{
    
    
	int n;
	cin>>n;
	int cnt=0;
	while(n)
	{
    
    
		n=(n-1)&n;
		cnt++;
	} 
	cout<<cnt<<endl;
	return 0;
 } 

Guess you like

Origin blog.csdn.net/weixin_45102820/article/details/113049036