当前位置:网站首页>Neglected technique: bit operation
Neglected technique: bit operation
2022-06-11 22:05:00 【Chrn morning】
Summary of basic operation knowledge of bit operation
1. Move left operation A<<B
take A Each bit of the binary representation of is shifted to the left B position , Cut off the excess bits on the left , The insufficient position on the right 0
A = 1100 B = 2
A << B = 110000
2. Move right A>>B,A>>>B
The right shift operation is divided into arithmetic right shift and logical right shift
Arithmetic right shift is a signed right shift , Logical right shift is an unsigned right shift .
Arithmetic shift right : take A Each bit of the binary representation of is shifted to the right B position , The excess bits on the right are cut off , The number of sign bits filled by the insufficient bits on the left .
Logical shift right : take A Each bit of the binary representation of is shifted to the right B position , The excess bits on the right are cut off , The insufficient space on the left makes up 0.
C Language : Logical shift right only A >> B
Arithmetic shift right A >> B , Logical shift right A >>> B
A = 1111111111111111110000001
B = 2
A >> B = 1111111111111111111100000
A >>> B = 0011111111111111111100000
3. Press bit and operate A&B
take A and B Each bit of the binary representation of , Only two corresponding bits are 1 when , The result is 1, Otherwise 0.
A = 001010
B = 101100
A & B = 001000
4. To press or operate A|B
take A and B Each bit of the binary representation of a , As long as one of the two corresponding binary bits is 1, The result bit is 1, Otherwise 0.
A = 001010
B = 101100
A | B = 101110
5. Bitwise non operation ~A
take A The binary representation of each bit negates , If the corresponding binary bit is 0, The result bit is 1, Otherwise 0.
A =01010
~A = 10101
6. Operate by bitwise exclusive or A^B
take A and B Each bit of the binary representation of the , If the corresponding bits are different , The result bit is 1, Otherwise 0.
A = 001010
B = 101100
A ^ B = 100110
Summary of topics related to bit operation :
1.1Single Number/Single Number II
In an array, except one number appears once , The rest of the numbers appear twice , Find the number that only appears once .
C The language code :
int number(vector<int> &num)
{
int result=0;
for(int a:num)
{
result^a;
}
return result;
}
explain : Exclusive or operation :
1.a ^ a=0
2.a ^ 0=a
3.a ^ b ^ a=b ( A number exclusive or the same number can be restored twice )
If only one number appears once in an array , According to the above 1 and 2 You know , The XOR result of two identical numbers is 0, Then the number XOR appearing twice in the array is 0, There is only one occurrence left .
1.2 In a set of numbers , There is a number that appears only once , The rest of them came out three times , Find the number that only appears once .
The solution is similar to the above problem
2. The binary form of a decimal number 1 The number of
And operation
Use and operation 0&1=0, Continue to convert a non-zero number into a binary form 1 in 0
int get_one_num(int n){
int res = 0;
while(n){
n = n&(n-1);
res++;
}
return res;
}
3. Judge whether a number is 2 The power of , such as 1=2^0, 2=2^1, 4=2^2
And operation
Utilization and operation ,2&1=0,4&3=0,2 The binary form of the power of must be that the first digit is 1, For the other 0, And its minus one form must be that this one is 0, For the other 1.
for example :8 The binary of is 1000 and 7 The binary of is 111,4 The binary of is 100,3 The binary of is 11
boolean istwores(int n){
return n&(n-1)==0?True:False;
}
4. Because bit operations are faster than multiplication , So we can x = x * 2 Optimize to x = x << 1.
x=x/2 Optimize to x=x>>1, Improved computing speed
5、 Judge the two numbers entered m、n How many bits are different in the binary bits of , First m and n Do bitwise XOR , Set the different parts of the binary representation of the two to 1, Same partial setting 0, Get a number diff, Then count diff How many bits are there in the binary representation of 1.
int differences(int m, int n)
{
int diff = m ^ n;
int count = 0;
while(diff)
{
diff = diff & (diff - 1);
count++;
}
return count;
}
6. No new variables , Exchange the values of two variables
Based on XOR operation
a=a^b;
b=a^b;
a=a^b;
for example : a=4( Binary is 100) b=3( Binary is 11)
a=a ^ b=4 ^ 3=7
b=a ^ b=7 ^ 3=4
a=a ^ b=7 ^ 4=3
summary
Question type of bit operation :
1. Hexadecimal conversion
2. Binary 1 The number of
3. A statement determines whether an integer is 2 Integer power of
4. One number changes several digits to get another number :
5. The number of occurrences of numbers in an array
6. Do not add, subtract, multiply or divide
边栏推荐
猜你喜欢

Win10弹出USB时出现该设备正在使用的解决方法

The upcoming launch of the industry's first retail digital innovation white paper unlocks the secret of full link digital success

科普 | NFT的类型有哪些(上)

快速排序的优化

inner join执行计划变了

Popular science | what are the types of NFT (Part 1)

How to use the transaction code sat to find the name of the background storage database table corresponding to a sapgui screen field

Glory earbud 3 Pro with three global first strong breakdowns flagship earphone Market

机器学习之线性回归简单实例

One question per day -- verifying palindrome string
随机推荐
快速排序的非递归写法
3.2 测试类的命名规则
每日一题-删除有序数组的重复项
Superscalar processor design yaoyongbin Chapter 2 cache -- Excerpt from subsection 2.3
win10字体模糊怎么调节
MySQL事务简介
One question per day -- verifying palindrome string
类和对象(1)
判断大小端存储两种办法
C语言实现八种排序 - 归并排序
重温c语言一
剑指Offer 29.顺时针打印矩阵
Dynamic memory management (1)
R语言相关文章、文献整理合集(持续更新)
Prefabricated dishes in the trillion market have also begun to roll inside. How can brands stand out in the fierce competition?
6.项目上线
超标量处理器设计 姚永斌 第2章 Cache --2.2 小节摘录
[Yu Yue education] basic engineering English of Zhejiang industrial and Commercial University (wuyiping) reference materials
How to view the installation date of the win system
69. x的平方根