Leetcode: Sum of Two Integers && Summary: Bit Manipulation
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Example:
Given a = 1 and b = 2, return 3.
转自https://discuss.leetcode.com/topic/49771/java-simple-easy-understand-solution-with-explanation/2,注意里面对于减法的讲解
have been confused about bit manipulation for a very long time. So I decide to do a summary about it here.
"&" AND operation, for example, 2 (0010) & 7 (0111) => 2 (0010)
"^" XOR operation, for example, 2 (0010) ^ 7 (0111) => 5 (0101)
"~" NOT operation, for example, ~2(0010) => -3 (1101) what??? Don't get frustrated here. It's called two's complement.
1111 is -1, in two's complement
1110 is -2, which is ~2 + 1, ~0010 => 1101, 1101 + 1 = 1110 => 2
1101 is -3, which is ~3 + 1
so if you want to get a negative number, you can simply do ~x + 1
Reference:
https://en.wikipedia.org/wiki/Two%27s_complement
https://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html
For this, problem, for example, we have a = 1, b = 3,
In bit representation, a = 0001, b = 0011,
First, we can use "and"("&") operation between a and b to find a carry.
carry = a & b, then carry = 0001
Second, we can use "xor" ("^") operation between a and b to find the different bit, and assign it to a,
Then, we shift carry one position left and assign it to b, b = 0010.
Iterate until there is no carry (or b == 0)
// Iterative
public int getSum(int a, int b) {
while (b != 0) {
int carry = a & b;
a = a ^ b;
b = carry << 1;
} return a;
} // Iterative
public int getSubtract(int a, int b) {
while (b != 0) {
int borrow = (~a) & b;
a = a ^ b;
b = borrow << 1;
} return a;
} // Recursive
public int getSum(int a, int b) {
return (b == 0) ? a : getSum(a ^ b, (a & b) << 1);
} // Recursive
public int getSubtract(int a, int b) {
return (b == 0) ? a : getSubtract(a ^ b, (~a & b) << 1);
} // Get negative number
public int negate(int x) {
return ~x + 1;
}
Leetcode: Sum of Two Integers && Summary: Bit Manipulation的更多相关文章
- LeetCode——Sum of Two Integers
LeetCode--Sum of Two Integers Question Calculate the sum of two integers a and b, but you are not al ...
- [LeetCode] Sum of Two Integers 两数之和
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
- LeetCode Sum of Two Integers
原题链接在这里:https://leetcode.com/problems/sum-of-two-integers/ 题目: Calculate the sum of two integers a a ...
- LeetCode 371. Sum of Two Integers (两数之和)
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
- Leetcode: Range Sum Query 2D - Mutable && Summary: Binary Indexed Tree
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- 【一天一道LeetCode】#371. Sum of Two Integers
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Calcula ...
- LeetCode Javascript实现 344. Reverse String 292. Nim Game 371. Sum of Two Integers
344. Reverse String /** * @param {string} s * @return {string} */ var reverseString = function(s) { ...
- 通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)
昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers Calculate the sum of two integers a and b, b ...
- 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)
剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...
随机推荐
- Python基础Day8
一.内容回顾 列表的存储:列表里的元素存储的是值的内存地址,就算通过copy,复制后容器里的值也是指向同一个内存地址(跟驻留机制有关) l1 = [1,2,3, l2 = l1.copy() 浅cop ...
- 【OF框架】使用原生Sql查询返回实体
使用原生Sql查询为Entity Framework Core自身的能力,本处描述如何在框架中调用该能力. 框架代码如下: (IoCHelper.Resolve<IDbContextCore&g ...
- 运输层3——传输控制协议TCP概述
目录 1. TCP最主要的特点 2. TCP的连接 3. socket在不同场景中的含义 写在前面:本文章是针对<计算机网络第七版>的学习笔记 运输层1--运输层协议概述 运输层2--用户 ...
- BZOJ 2321 星器
星器 思路: 势能分析法. 假设每颗星星的势能为\(x^2+y^2\) 那么对于一行的两颗星星\((i, j), (i, k), j < k\) 它转移到\((i, j+1), (i, k-1) ...
- python访问aws-S3服务
创建本地 AWS 凭证文件 登录 AWS 管理控制台 并通过以下网址打开 IAM 控制台 https://console.amazonaws.cn/iam/. 创建一个新用户,其权限仅限于您希望您的代 ...
- C++——宏观把控
跟看所有的书一样,我们都要求第一遍泛读,宏观把控书本内容,C++依旧如此进行.看到前面这几章的时候感觉非常熟悉,因为能让我联想到很多以前学习的VB.C#等的知识,感觉轻松很多,原来我已经学过了很多东西 ...
- Selenium常用API的使用java语言之4-环境安装之Selenium
1.通过jar包安装 点击Selenium下载 链接 你会看到Selenium Standalone Server的介绍: The Selenium Server is needed in order ...
- 基于css文件编写一个简单的html前端页面
因为文本原因,文件不能直接上传,以压缩包的形式上传上来 参考下载路径:https://i-beta.cnblogs.com/files
- 记录从裸机到TensorFlow GPU版运行 的配置过程
实验室原来有一台装Ubuntu Server系统的服务器,安装有tensorflow,在使用过程中经常出现断网.死机.自动关机等毛病,忍无可忍,决定重装系统 配置如下:Dell工作站,Xeon-E5 ...
- SIGAI机器学习第三集 数学知识-2
讲授机器学习相关的高等数学.线性代数.概率论知识 大纲: 最优化中的基本概念梯度下降法牛顿法坐标下降法数值优化算法面临的问题拉格朗日乘数法凸优化问题凸集凸函数凸优化拉格朗日对偶KKT条件 最优化中的基 ...