当前位置:网站首页>位运算例题(待续)
位运算例题(待续)
2022-06-12 15:39:00 【General_zy】
基本原理
0s 表示一串 0,1s 表示一串 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
位 1 的个数
- 编写一个函数,输入是一个无符号整数(以二进制串的形式),返回其二进制表达式中数字位数为 ‘1’ 的个数(也被称为汉明重量)。
- 输入必须是长度为 32 的 二进制串 。
循环每一位
func hammingWeight(num uint32) int {
count :=0
for i:=0;i<32;i++{
// 此处结果为uint32要转为int
count+= int((num>>i)&1)
}
return count
}
位运算性质
对于整数 n,n & (n−1) 的结果为将 n 的二进制表示的最后一个 1 变成 0。
func hammingWeight(num uint32) int {
count :=0
for num!=0{
num=num&(num-1)
count++
}
return count
}
待续
边栏推荐
- 2021-06-20
- Defer learning in golang
- Leetcode daily question - fair candy bar exchange
- Module yaml error: Unexpected key in data: static_ context [line 9 col 3]
- UDP summary (tcp/ip details volume 1/2)
- The nohup command uses
- Raccourci vers le nouvel environnement du carnet de notes Jupiter
- Saga体系结构模式:微服务架构下跨服务事务的实现
- How to use grafana to easily realize OVL data visualization
- Codeworks round 797 (Div. 3, cf1690)
猜你喜欢

FPGA (III) trigger and latch

Kinect2.0+ORBSLAM2_ with_ pointcloud_ map
![CUDA out of memory or brokenpipeerror: [errno 32] broken pipe or oserror: [winerror 1455] solution to the problem that the page file is too small](/img/2c/63f1c865105a74ed651bb0ef97ab60.png)
CUDA out of memory or brokenpipeerror: [errno 32] broken pipe or oserror: [winerror 1455] solution to the problem that the page file is too small

5g new scheme! Upgrade the existing base station and UE simulator to 5g millimeter wave band

UDP summary (tcp/ip details volume 1/2)
![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] 页面文件太小的解决办法

华为设备配置CE双归属

redis String类型常见命令

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

如何使用Grafana轻松实现OVL数据可视化
随机推荐
Multi thread knowledge induction
Unity get local video / download network video
Five models of software testing
任务 水果炸汁机 0611
File uploading and downloading in SSM
ROS beginners write the server that the little turtle rotates a certain angle at a certain speed
Deepin20.6 rtx3080 installer le lecteur de carte graphique 510.60.02, cuda 11.6, pytorch1.11
First set and follow set in vernacular
【架构优化过程思考】如何构建技术方案影响的评估能力
如何使用Grafana轻松实现OVL数据可视化
Solution of user and root forgetting password in virtual machine
IMU learning records
Introduction to resttemplate
Rust tip - running the tensorrt model through FFI programming
作業提交說明 上傳作業到網盤中
jupyter notebook新环境快捷方式
C language partition bin file program
Raccourci vers le nouvel environnement du carnet de notes Jupiter
mysql Blob和Text类型
Seaborn Brief