当前位置:网站首页>[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;
}
边栏推荐
- 2022/2/12 summary
- Matlab learning notes
- Meet the level 3 requirements of ISO 2.0 with the level B construction standard of computer room | hybrid cloud infrastructure
- 5种不同的代码相似性检测,以及代码相似性检测的发展趋势
- Leecode brushes questions and records interview questions 01.02 Determine whether it is character rearrangement for each other
- [daily problem insight] prefix and -- count the number of fertile pyramids in the farm
- 基于GO语言实现的X.509证书
- Leecode brush questions record interview questions 32 - I. print binary tree from top to bottom
- 37 pages Digital Village revitalization intelligent agriculture Comprehensive Planning and Construction Scheme
- Understand the misunderstanding of programmers: Chinese programmers in the eyes of Western programmers
猜你喜欢
2022/2/12 summary
Mujoco finite state machine and trajectory tracking
Hero League | King | cross the line of fire BGM AI score competition sharing
【批处理DOS-CMD命令-汇总和小结】-字符串搜索、查找、筛选命令(find、findstr),Find和findstr的区别和辨析
37頁數字鄉村振興智慧農業整體規劃建設方案
Business process testing based on functional testing
@TableId can‘t more than one in Class: “com.example.CloseContactSearcher.entity.Activity“.
Data processing of deep learning
Article management system based on SSM framework
AI super clear repair resurfaces the light in Huang Jiaju's eyes, Lecun boss's "deep learning" course survival report, beautiful paintings only need one line of code, AI's latest paper | showmeai info
随机推荐
37 pages Digital Village revitalization intelligent agriculture Comprehensive Planning and Construction Scheme
什么是时间
Explain in detail the implementation of call, apply and bind in JS (source code implementation)
Understand the misunderstanding of programmers: Chinese programmers in the eyes of Western programmers
Leecode brush questions record sword finger offer 11 Rotate the minimum number of the array
Zynq transplant ucosiii
深度学习之数据处理
Leecode brush questions record sword finger offer 43 The number of occurrences of 1 in integers 1 to n
PXE server configuration
【批处理DOS-CMD命令-汇总和小结】-跳转、循环、条件命令(goto、errorlevel、if、for[读取、切分、提取字符串]、)cmd命令错误汇总,cmd错误
浅谈测试开发怎么入门,如何提升?
Alexnet experiment encounters: loss Nan, train ACC 0.100, test ACC 0.100
以机房B级建设标准满足等保2.0三级要求 | 混合云基础设施
Advanced learning of MySQL -- basics -- transactions
37 page overall planning and construction plan for digital Village revitalization of smart agriculture
Win10 startup error, press F9 to enter how to repair?
JS+SVG爱心扩散动画js特效
MySQL learning notes (mind map)
代码克隆的优缺点
Model-Free Control