当前位置:网站首页>[Niuke classic question 01] bit operation
[Niuke classic question 01] bit operation
2022-07-07 00:49:00 【RookieStriver】
Here's the catalog title
JZ65 Do not add, subtract, multiply or divide
describe :
Write a function , Find the sum of two integers , It is required that... Should not be used in the function body +、-、*、/ Four operation symbols .
Data range : Both numbers satisfy -10 \le n \le 1000−10≤n≤1000
Advanced : Spatial complexity O(1)O(1), Time complexity O(1)O(1)
Example 1:
Input :1,2
Return value :3
Example 2:
Input :0,0
Return value :0
Algorithm ideas :
Decimal addition thought : 5+7=12, Three steps
First step : Add the values of each , It's not a carry , obtain 2.
The second step : Calculate the carry value , obtain 10. If the carry value of this step is 0, So the value of the first step is the final result .
The third step : Repeat the above two steps , Only the added value becomes the result of the above two steps 2 and 10, obtain 12.
Binary addition thought : Same as decimal , First calculate the addition result without considering carry (0+0 have to 0,1+1 Carried 0,1+0 have to 1), Use XOR to get ; Then calculate the carry result of the addition ( Same as 1 Move the position of the left one bit ), Use phase and shift back left to get .
Sample code :
int Add(int num1, int num2)
{
while (num2 != 0)// Carry is not 0 Continue to add with the addition result
{
int sum = num1 ^ num2;// Get the data of each addition regardless of carry
int temp = (num1 & num2) << 1; // Adding the same bits will carry
num1 = sum;
num2 = temp;
}
return num1;
}
int main()
{
int num1 = 0, num2 = 0;
scanf("%d%d", &num1, &num2);
int ret = Add(num1, num2);
printf("%d\n", ret);
system("pause");
return 0;
}
边栏推荐
- Zynq transplant ucosiii
- Are you ready to automate continuous deployment in ci/cd?
- Liuyongxin report | microbiome data analysis and science communication (7:30 p.m.)
- Attention slam: a visual monocular slam that learns from human attention
- 深度学习之数据处理
- Js+svg love diffusion animation JS special effects
- Value Function Approximation
- Leetcode (547) - number of provinces
- Mujoco Jacobi - inverse motion - sensor
- Core knowledge of distributed cache
猜你喜欢
48 page digital government smart government all in one solution
Equals() and hashcode()
集合(泛型 & List & Set & 自定义排序)
Core knowledge of distributed cache
uniapp实现从本地上传头像并显示,同时将头像转化为base64格式存储在mysql数据库中
St table
JWT signature does not match locally computed signature. JWT validity cannot be asserted and should
Linear algebra of deep learning
On February 19, 2021ccf award ceremony will be held, "why in Hengdian?"
File and image comparison tool kaleidoscope latest download
随机推荐
Interesting wine culture
【JokerのZYNQ7020】AXI_ EMC。
Geo data mining (III) enrichment analysis of go and KEGG using David database
Slam d'attention: un slam visuel monoculaire appris de l'attention humaine
[daily problem insight] prefix and -- count the number of fertile pyramids in the farm
Linear algebra of deep learning
Idea automatically imports and deletes package settings
Jenkins' user credentials plug-in installation
建立自己的网站(17)
Telerik UI 2022 R2 SP1 Retail-Not Crack
fastDFS数据迁移操作记录
Leecode brush questions record sword finger offer 44 A digit in a sequence of numbers
509 certificat basé sur Go
If the college entrance examination goes well, I'm already graying out at the construction site at the moment
2022/2/10 summary
Leecode brush questions record sword finger offer 11 Rotate the minimum number of the array
Amazon MemoryDB for Redis 和 Amazon ElastiCache for Redis 的内存优化
equals()与hashCode()
uniapp实现从本地上传头像并显示,同时将头像转化为base64格式存储在mysql数据库中
ActiveReportsJS 3.1中文版|||ActiveReportsJS 3.1英文版