当前位置:网站首页>Sword finger offer 15.65.56 I 56Ⅱ. Bit operation (simple - medium)
Sword finger offer 15.65.56 I 56Ⅱ. Bit operation (simple - medium)
2022-06-26 14:14:00 【hedgehog:】
Combined with another bit operation blog post :
15.
subject :


idea : Traverse every bit , Judge whether it is bit 1, Add up .
Code :
public class Solution {
// you need to treat n as an unsigned value
// n An unsigned number
public int hammingWeight(int n) {
int cnt=0;
for(int i=0;i<32;i++){
// This one is 1
if(((n>>i)&1)==1)
cnt++;
}
return cnt;
}
}result :

65.
subject :

idea : Traverse each bit of the two addends , Record this bit 1 The number of , And add carry . Yes 1 Number judgment of , And respond to the operation ( See the code ).
Code :
class Solution {
public int add(int a, int b) {
int addOne=0;// carry
int res=0;// result
for(int i=0;i<32;i++){
int cntOne=0;
// If a This bit is 1
if(((a>>i)&1)==1)
cntOne++;
// If b This bit is 1
if(((b>>i)&1)==1)
cntOne++;
// Plus carry
cntOne+=addOne;
if(cntOne==2){ // This bit has two 1
// Make this bit 0, Carry is 1
// res|=(0<<i);
addOne=1;
}
else if(cntOne==3){ // This bit has two 1 And a carry
// Make this bit 1, Carry is 1
res|=(1<<i);
addOne=1;
}else{ //if(cntOne<=1) This bit has a 1 Or not 1( Two 0)
// Make this bit 0 or 1( namely cntOne Value ), No carry
res|=(cntOne<<i);
addOne=0;
}
}
return res;
}
}result :

56Ⅰ.56Ⅱ.
See another post :
边栏推荐
- 证券开户安全吗,有没有什么危险啊
- Obtain information about hard disk and volume or partition (capacity, ID, volume label name, etc.)
- Variable declaration of typescript
- 服务器创建虚拟环境跑代码
- Build your own PE manually from winpe of ADK
- Stream常用操作以及原理探索
- 7.Consul服务注册与发现
- It is better and safer to choose which securities company to open an account for flush stock
- Luogu p4513 xiaobaiguang Park
- mysql配置提高数据插入效率
猜你喜欢

BP neural network for prediction

7.consul service registration and discovery

Pointer

Freefilesync folder comparison and synchronization software

Pytorch based generation countermeasure Network Practice (7) -- using pytorch to build SGAN (semi supervised GaN) to generate handwritten digits and classify them

Zero basics of C language lesson 7: break & continue

Win10 home vs pro vs enterprise vs enterprise LTSC

Codeforces Global Round 21A~D

Sword finger offer 06.24.35 Linked list

Sword finger offer 10 Ⅰ 10Ⅱ. 63 dynamic planning (simple)
随机推荐
Wechat applet Registration Guide
虫子 内存管理 下 内存注意点
The most critical elements of team management
Freefilesync folder comparison and synchronization software
Linear basis count (k large XOR sum)
Bug memory management
MySQL configuration improves data insertion efficiency
Research on balloon problem
C language ---getchar() and putchar()
Sword finger offer 09.30 Stack
Assert and constd13
【系统分析师之路】第十五章 复盘数据库系统(数据库案例分析)
How to call self written functions in MATLAB
Comparison of disk partition modes (MBR and GPT)
Reprint - easy to use wechat applet UI component library
同花顺股票开户选哪个证券公司是比较好,比较安全的
GEE——全球人类居住区网格数据 1975-1990-2000-2014
Wechat applet SetData dynamic variable value sorting
When drawing with origin, capital letter C will appear in the upper left corner of the chart. The removal method is as follows:
常用控件及自定义控件
https://leetcode-cn.com/problems/er-jin-zhi-zhong-1de-ge-shu-lcof/