当前位置:网站首页>不用加减乘除实现加法
不用加减乘除实现加法
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
边栏推荐
- Common interview questions for performance test
- Const and the secret of pointers
- # 使用 KubeKey 搭建 Kubernetes/KubeSphere 环境的'心路(累)历程'
- split(),splice(),slice()傻傻分不清楚?
- Communication protocol -- Classification and characteristics Introduction
- Hello World generation
- Design of serial port receiving data scheme
- Introduction to ieda right click source file menu
- C#实现图的深度优先遍历--非递归代码
- Introduction to core functions of webrtc -- an article on understanding SDP PlanB unifiedplan (migrating from PlanB to unifiedplan)
猜你喜欢

So easy deploy program to server

雪崩问题以及sentinel的使用

POI exports excel and displays hierarchically according to parent-child nodes

So easy 将程序部署到服务器

# 使用 KubeKey 搭建 Kubernetes/KubeSphere 环境的'心路(累)历程'

Huawei operator level router configuration example | configuration optionA mode cross domain LDP VPLS example

如何校验两个文件内容是否相同

The best learning method in the world: Feynman learning method

伺服第二编码器数值链接到倍福PLC的NC虚拟轴做显示
![[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
随机推荐
A few lines of transaction codes cost me 160000 yuan
md5sum操作
【小程序项目开发-- 京东商城】uni-app之首页商品楼层
EtherCAT原理概述
多元线性回归
Hal library setting STM32 interrupt
Introduction and installation of Solr
Md5sum operation
Detailed list of errors related to twincat3 ads of Beifu
伺服第二编码器数值链接到倍福PLC的NC虚拟轴做显示
第03章_用戶與權限管理
Redis tutorial
IEDA 右键源码文件菜单简介
C#实现图的深度优先遍历--非递归代码
世界上最好的学习法:费曼学习法
Common interview questions for performance test
Huawei operator level router configuration example | configuration optionA mode cross domain LDP VPLS example
Druid monitoring statistics source
How to verify whether the contents of two files are the same
xxl-job使用指南