当前位置:网站首页>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
边栏推荐
- Cygwin的下载和安装配置
- Edlines: a real time line segment detector with a false detection control
- Md5sum operation
- Develop industrial Internet with the technical advantages of small programs
- 文件上传下载
- split(),splice(),slice()傻傻分不清楚?
- Split(), split(), slice(), can't you tell?
- ECMAScript 6.0
- pytorch nn. AdaptiveAvgPool2d(1)
- Feature Pyramid Networks for Object Detection论文理解
猜你喜欢

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

Overview of EtherCAT principle

家居网购项目

还在浪费脑细胞自学吗,这份面试笔记绝对是C站天花板

Listener listener

复习专栏之---消息队列

终极套娃 2.0 | 云原生交付的封装

文件上传下载

Thread data sharing and security -threadlocal

Ridge regression and lasso regression
随机推荐
Edge drawing: a combined real-time edge and segment detector
Finally in promise
The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)
Valid brackets (force deduction 20)
Design of serial port receiving data scheme
ECMAScript 6.0
文件上传下载
TEC: Knowledge Graph Embedding with Triple Context
C language EXECL function
终极套娃 2.0 | 云原生交付的封装
Introduction and installation of Solr
力扣-两数之和
Home online shopping project
Nacos
Subnet division and subnet summary
leetcode 1482 猜猜看啊,这道题目怎么二分?
Edge Drawing: A combined real-time edge and segment detector 翻译
Leetcode 1482 guess, how about this question?
10、Scanner.next() 无法读取空格/indexOf -1
torch.histc