当前位置:网站首页>Addition without addition, subtraction, multiplication and division
Addition without addition, subtraction, multiplication and division
2022-07-01 03:36:00 【Enthusiastic citizen Mr. Xue】

Bit operation is used here ,a+b The result of no carry and a^b equal ,0 + 1 = 1 == 0 ^ 1 = 1
a+b The carry of is equal to (a&b) << 1 3+1 The carry of is (11 & 1) << 1 = 10
public int add(int a, int b) {
// Carry is 0 Jump out when
while(b != 0){
// carry
int c = (a & b) << 1;
// No carry and
a = a^b;
// carry
b = c;
}
return a;
}
operation 3+1:
1 != 0 Into the loop carry c = (11 & 1) << 1 = 10
a = 11 ^ 1 = 10
b = 10The second cycle 10 != 0
c = (10 & 10) << 1 = 100
a = 10 ^ 10 = 0
b = 100The third cycle 100 ! = 0
c = ( 0 & 100) << 1 = 0
a = a ^ 100 = 100
b = 0;end The result is 4
边栏推荐
- Leetcode:829. 连续整数求和
- Analyze datahub, a new generation metadata platform of 4.7K star
- TEC: Knowledge Graph Embedding with Triple Context
- Asgnet paper and code interpretation 2
- Explain spark operation mode in detail (local+standalone+yarn)
- Hal library operation STM32 serial port
- Subnet division and subnet summary
- Learning notes for introduction to C language multithreaded programming
- Leetcode 1482 guess, how about this question?
- Appium自动化测试基础--补充:C/S架构和B/S架构说明
猜你喜欢

File upload and download

Basic concepts of database

Appium自动化测试基础 — APPium基本原理

深度学习中的随机种子torch.manual_seed(number)、torch.cuda.manual_seed(number)

Feign远程调用和Getaway网关

完全背包问题
![[us match preparation] complete introduction to word editing formula](/img/e4/5ef19d52cc4ece518e79bf10667ef4.jpg)
[us match preparation] complete introduction to word editing formula

TEC: Knowledge Graph Embedding with Triple Context
![[nine day training] content III of the problem solution of leetcode question brushing Report](/img/7e/1e76181e56ef7feb083f9662df71c7.jpg)
[nine day training] content III of the problem solution of leetcode question brushing Report

数据库中COMMENT关键字的使用
随机推荐
How do spark tasks of 10W workers run? (Distributed Computing)
Let's just say I can use thousands of expression packs
Pyramid scene parsing network [pspnet] thesis reading
C # realize solving the shortest path of unauthorized graph based on breadth first BFS -- complete program display
Home online shopping project
Nacos
Introduction and installation of Solr
Depth first traversal of C implementation Diagram -- non recursive code
Research on target recognition and tracking based on 3D laser point cloud
网页不能右键 F12 查看源代码解决方案
Ridge regression and lasso regression
pytorch中的双线性插值上采样(Bilinear Upsampling)、F.upsample_bilinear
Appium自动化测试基础--补充:C/S架构和B/S架构说明
ASGNet论文和代码解读2
leetcode 1818 绝对值,排序,二分法,最大值
数据交换 JSON
后台系统右边内容如何出现滚动条和解决双滚动条的问题
完全背包问题
Golang multi graph generation gif
Basic concepts of database