当前位置:网站首页>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
边栏推荐
- 串口接收数据方案设计
- Avalanche problem and the use of sentinel
- IPv4和IPv6、局域网和广域网、网关、公网IP和私有IP、IP地址、子网掩码、网段、网络号、主机号、网络地址、主机地址以及ip段/数字-如192.168.0.1/24是什么意思?
- [深度学习]激活函数(Sigmoid等)、前向传播、反向传播和梯度优化;optimizer.zero_grad(), loss.backward(), optimizer.step()的作用及原理
- pytest-fixture
- IPv4 and IPv6, LAN and WAN, gateway, public IP and private IP, IP address, subnet mask, network segment, network number, host number, network address, host address, and IP segment / number - what does
- 多元线性回归
- Leetcode:剑指 Offer 59 - I. 滑动窗口的最大值
- Analyze datahub, a new generation metadata platform of 4.7K star
- How do I use Google Chrome 11's Upload Folder feature in my own code?
猜你喜欢

The preorder traversal of leetcode 144 binary tree and the expansion of leetcode 114 binary tree into a linked list

How to achieve 0 error (s) and 0 warning (s) in keil5

AfxMessageBox和MessageBox的用法

静态库使用MFC和共享库使用MFC的区别

EDLines: A real-time line segment detector with a false detection control翻译

Are you still wasting brain cells for self-study? This interview note is definitely the ceiling of station C

Avalanche problem and the use of sentinel

二叉树神级遍历:Morris遍历

IPv4 and IPv6, LAN and WAN, gateway, public IP and private IP, IP address, subnet mask, network segment, network number, host number, network address, host address, and IP segment / number - what does

Ctfshow blasting WP
随机推荐
How do spark tasks of 10W workers run? (Distributed Computing)
pytorch nn.AdaptiveAvgPool2d(1)
衡量两个向量相似度的方法:余弦相似度、pytorch 求余弦相似度:torch.nn.CosineSimilarity(dim=1, eps=1e-08)
【读书笔记】《文案变现》——写出有效文案的四个黄金步骤
文件上传下载
Redis tutorial
[小样本分割]论文解读Prior Guided Feature Enrichment Network for Few-Shot Segmentation
LeetCode 144二叉树的前序遍历、LeetCode 114二叉树展开为链表
Feign remote call and getaway gateway
File upload and download
Thread data sharing and security -threadlocal
C语言的sem_t变量类型
Feign远程调用和Getaway网关
5、【WebGIS实战】软件操作篇——服务发布及权限管理
Edge drawing: a combined real-time edge and segment detector
About the application of MySQL
二叉树神级遍历:Morris遍历
还在浪费脑细胞自学吗,这份面试笔记绝对是C站天花板
Edge Drawing: A combined real-time edge and segment detector 翻译
Analyze datahub, a new generation metadata platform of 4.7K star