当前位置:网站首页>Example of bit operation (to be continued)
Example of bit operation (to be continued)
2022-06-12 15:47:00 【General_ zy】
The basic principle
0s A string of 0,1s A string of 1.
x ^ 0s = x x & 0s = 0 x | 0s = x
x ^ 1s = ~x x & 1s = x x | 1s = 1s
x ^ x = 0 x & x = x x | x = x
position 1 The number of
- Write a function , Input is an unsigned integer ( In the form of a binary string ), Returns the number of digits in its binary expression as ‘1’ The number of ( Also known as Han Ming weight ).
- The input must be of length 32 Of Binary string .
Loop each bit
func hammingWeight(num uint32) int {
count :=0
for i:=0;i<32;i++{
// The result here is uint32 Turn to int
count+= int((num>>i)&1)
}
return count
}
Bit operation properties
For integers n,n & (n−1) The result is that n The last binary representation of 1 become 0.
func hammingWeight(num uint32) int {
count :=0
for num!=0{
num=num&(num-1)
count++
}
return count
}
To be continued
边栏推荐
- 任务 水果炸汁机 0611
- Classic case of solidity - Smart games
- 5g new scheme! Upgrade the existing base station and UE simulator to 5g millimeter wave band
- 写代码也有本手俗手之分,而我们要善于发现妙手!
- nohup 命令使用
- Introduction to resttemplate
- Why doesn't Alibaba recommend MySQL use the text type?
- Redis string type common commands
- 第一章 线性表
- [practical case of light source] UV-LED curing innovation makes the production line more smooth
猜你喜欢

jupyter notebook新环境快捷方式

Servlet connects to database to realize user login function

【光源实用案例】 UV-LED固化创新,让产线变得更丝滑

一步步创建包含自定义 Screen 的 ABAP 程序的详细步骤
![CUDA out of memory 或 BrokenPipeError: [Errno 32] Broken pipe 或 OSError: [WinError 1455] 页面文件太小的解决办法](/img/2c/63f1c865105a74ed651bb0ef97ab60.png)
CUDA out of memory 或 BrokenPipeError: [Errno 32] Broken pipe 或 OSError: [WinError 1455] 页面文件太小的解决办法

Jupyter notebook new environment shortcut

From K-means to capsule

C language partition bin file program

应势而变,2022年下半场的升级之路

当编程纳入到高考。。。
随机推荐
[practical case of light source] UV-LED curing innovation makes the production line more smooth
Use of thread communication
Dart typedef的理解
Servlet知识详解(2)
UE4 common type conversion
2021-06-20
Golang collaboration scheduling (I): Collaboration Status
Apache Kylin 历险记
TCP与UDP的区别,以及TCP的三次握手和TCP的四次挥手
Use of multithreading
ARM 64指令小记
从斐波那契数列求和想到的俗手、本手和妙手
Fiddler packet capturing (mobile app)
Escape rules and examples of go
Microservice fault tolerance
【架构优化过程思考】如何构建技术方案影响的评估能力
Servlet knowledge explanation (2)
Module yaml error: Unexpected key in data: static_ context [line 9 col 3]
UDP summary (tcp/ip details volume 1/2)
Redis General Command