当前位置:网站首页>Bit operation skills
Bit operation skills
2022-07-05 04:06:00 【Phyllostachys pubescens】
-------------------------
Judge odd and even numbers
1110 1111
0001 0001
0000 0001 // if((a&1)==0){}
-------------------------
Exchange two numbers
1001 a
1010 b
0011 a=a^b
1010 b
1001 b=a^b
0011
1001
1010 a=a^b
----------------------------
Multiplication and division
100>>1 10 // 4->2
100<<1 1000 //4->8
--------------------------------
Exchange symbols
0 1000 Take the opposite
1 0111 +1
1 1000
---------------------------------
High low swap a=(a<<4)|(a>>4)
1100 1011
a>>4
0000 1100
a>>4
1011 0000
Take or
1011 1100
------------------------
Statistics 1 The number of
1011 a&(a-1)
1010
1010
1001 a&(a-1)
1000
One less for each execution 1
-----------------------------
-----------------------------
Binary addition 5+3
0101 0101
0011 0011
^ 0110 0001 <<1 0010
public int add(int a, int b){
int a=a^b;
int b=a&b;
b<<1;
if(b==0) return 0;
else{
return Add(a,b);
}
}
----------------------------------
Only once , Other numbers appear 2 Time
1101
0000
1101
0 And any number ^ For itself
1101
1101
0000
Two identical numbers ^ The result is 0
Method : use 0 And each value in the array ^ operation , The result of the operation is the number that occurs once
---------------------------------------------------------------
Only once , Other numbers appear 3 Time
In the specific operation implementation , The data in the array is given in the problem int Within limits ,
So we can implement int Of 32 Each bit is judged in turn 1 The remainder of the number of 3 Is it 1,
If 1 The result shows that the bit is 1 You can add the results . The final value is the answer 、
class Solution {
public int singleNumber(int[] nums) {
int value=0;
for(int i=0;i<32;i++)
{
int sum=0;
for(int num:nums)
{
if(((num>>i)&1)==1)
{
sum++;
}
}
if(sum%3==1)
value+=(1<<i);
}
return value;
}
}
// Bit operation to achieve multiplication and division
int a=2;
a>>1; // Move right 1 position , It's equivalent to dividing 2 , The result is 1
a<<1; // Move left 1 position , It's equivalent to riding 2 , The result is 4
// Bit operations swap two numbers , No need for temporary variables , Efficiency is higher than ordinary operation
void Swap(int &a, int &b){
a=a+b;
b=a-b;
a=a-b;
}
void swap(int &a,int &b){
a=(a^b);
b=(b^a);
a=(a^b);
}
// Judge odd and even numbers
if(0==(a&1){
// even numbers
}
// Bit operation exchange symbol
int reversal(int a){
return ~a +1;
}
// Bit operation to find the absolute value
int abs(int a){
int i=a>>31;
return i==0?a:(~a+1);
}
// Bit operation for high and low bit switching
unsigned short a=34520;
a=(a>>8)|(a<<8)
// Bit operation in binary reverse order
unsigned short a = 34520;
a = ((a & 0xAAAA) >> 1) | ((a & 0x5555) << 1);
a = ((a & 0xCCCC) >> 2) | ((a & 0x3333) << 2);
a = ((a & 0xF0F0) >> 4) | ((a & 0x0F0F) << 4);
a = ((a & 0xFF00) >> 8) | ((a & 0x00FF) << 8);
// In statistical binary 1 The number of
count = 0
while(a){
a = a & (a - 1); // One less for each execution 1
count++;
}
// Eliminate the last one 1
x=1100
x-1=1011
x&(x-1)=1000
// a&b&b =a
边栏推荐
- 优先使用对象组合,而不是类继承
- How to realize real-time audio and video chat function
- 长度为n的入栈顺序的可能出栈顺序
- 25K 入职腾讯的那天,我特么哭了
- Phpmailer reported an error: SMTP error: failed to connect to server: (0)
- 为什么百度、阿里这些大厂宁愿花25K招聘应届生,也不愿涨薪5K留住老员工?
- Clickhouse materialized view
- 阿里云ECS使用cloudfs4oss挂载OSS
- Clickpaas low code platform
- What is the reason why the webrtc protocol video cannot be played on the easycvr platform?
猜你喜欢

Ctfshow web entry code audit

Use threejs to create geometry and add materials, lights, shadows, animations, and axes
![[brush questions] BFS topic selection](/img/24/7ec68ef60f94f7b366054824223709.png)
[brush questions] BFS topic selection

Special Edition: spreadjs v15.1 vs spreadjs v15.0

如何实现实时音视频聊天功能

How is the entered query SQL statement executed?

灵魂三问:什么是接口测试,接口测试怎么玩,接口自动化测试怎么玩?

JWT vulnerability recurrence

About the project error reporting solution of mpaas Pb access mode adapting to 64 bit CPU architecture

The development of mobile IM based on TCP still needs to keep the heartbeat alive
随机推荐
Excuse me, my request is a condition update, but it is blocked in the buffer. In this case, can I only flush the cache every time?
Seven join join queries of MySQL
Uni app common functions /api
3. Package the bottom navigation tabbar
Study notes 7
Containerd series - what is containerd?
[PHP features - variable coverage] improper use, improper configuration and code logic vulnerability of the function
Open graph protocol
Online text line fixed length fill tool
Threejs implements labels and displays labels with custom styles
kubernetes集群之调度系统
PlasticSCM 企业版Crack
C语言课设:影院售票管理系统
Plasticscm enterprise crack
Threejs factory model 3DMAX model obj+mtl format, source file download
Looking back on 2021, looking forward to 2022 | a year between CSDN and me
Longyuan war "epidemic" 2021 network security competition web easyjaba
BDF application - topology sequence
我国算力规模排名全球第二:计算正向智算跨越
陇原战“疫“2021网络安全大赛 Web EasyJaba