当前位置:网站首页>不用加减乘除实现加法
不用加减乘除实现加法
2022-07-01 03:13:00 【热心市民薛先生】

此处采用位运算,a+b无进位的结果和a^b相等,0 + 1 = 1 == 0 ^ 1 = 1
a+b的进位等于 (a&b) << 1 3+1的进位为 (11 & 1) << 1 = 10
public int add(int a, int b) {
//进位为0时跳出
while(b != 0){
//进位
int c = (a & b) << 1;
//无进位和
a = a^b;
//进位
b = c;
}
return a;
}
运算3+1:
1 != 0 进入循环 进位c = (11 & 1) << 1 = 10
a = 11 ^ 1 = 10
b = 10第二次循环 10 != 0
c = (10 & 10) << 1 = 100
a = 10 ^ 10 = 0
b = 100第三次循环 100 ! = 0
c = ( 0 & 100) << 1 = 0
a = a ^ 100 = 100
b = 0;结束 结果为 4
边栏推荐
- Multithreaded printing
- [linear DP] shortest editing distance
- # 使用 KubeKey 搭建 Kubernetes/KubeSphere 环境的'心路(累)历程'
- 8 pits of redis distributed lock
- STM32 - DS18B20 temperature sampling of first-line protocol
- Druid monitoring statistics source
- POI exports excel and displays hierarchically according to parent-child nodes
- HTB-Lame
- Basic concept and classification of sorting
- POI导出excel,按照父子节点进行分级显示
猜你喜欢

Detailed list of errors related to twincat3 ads of Beifu

The 'mental (tiring) process' of building kubernetes/kubesphere environment with kubekey
![[us match preparation] complete introduction to word editing formula](/img/e4/5ef19d52cc4ece518e79bf10667ef4.jpg)
[us match preparation] complete introduction to word editing formula

A few lines of transaction codes cost me 160000 yuan

Keil5中如何做到 0 Error(s), 0 Warning(s).
![[machine learning] vectorized computing -- a must on the way of machine learning](/img/3f/d672bb254f845ea705b3a0ca10ee19.png)
[machine learning] vectorized computing -- a must on the way of machine learning

第03章_用户与权限管理

最新接口自动化面试题
![Lavaweb [first understanding the solution of subsequent problems]](/img/8a/08cb2736c2c198d926dbe00c004c3f.png)
Lavaweb [first understanding the solution of subsequent problems]

Redis tutorial
随机推荐
【读书笔记】《文案变现》——写出有效文案的四个黄金步骤
手把手带你了解一块电路板,从设计到制作(干货)
线程数据共享和安全 -ThreadLocal
Elk elegant management server log
How the network is connected: Chapter 2 (Part 2) packet receiving and sending operations between IP and Ethernet
C#实现基于广度优先BFS求解无权图最短路径----完整程序展示
Common interview questions for performance test
8 pits of redis distributed lock
leetcode 1818 绝对值,排序,二分法,最大值
Redis 教程
Redis tutorial
【小程序项目开发-- 京东商城】uni-app之分类导航区域
The value of the second servo encoder is linked to the NC virtual axis of Beifu PLC for display
Data exchange JSON
Const and the secret of pointers
[applet project development -- JD mall] uni app commodity classification page (Part 2)
Promise中finally的用法
Design of serial port receiving data scheme
[QT] add knowledge supplement of third-party database
gcc使用、Makefile总结