371.用位运算实现加法 Sum of Two Integers

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.

Credits:
Special thanks to @fujiaozhu for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

  1. public class Solution {
  2. public int GetSum(int a, int b) {
  3. int sum = 0;
  4. while (b != 0) {
  5. sum = a ^ b;
  6. b = (a & b) << 1;
  7. a = sum;
  8. }
  9. return sum;
  10. }
  11. }

来自为知笔记(Wiz)

时间: 2024-08-11 03:41:35

371.用位运算实现加法 Sum of Two Integers的相关文章

LeetCode:位运算实现加法

LeetCode:位运算实现加法 写在前面 位运算符 实现加法的思路 两个加数,比如5(101)和6(110),如何不用加法就能得出两者之和呢? 我们知道二进制计算中,如果使用异或将会产生无进位的两者之和,而两数相与将会产生进位值!!! 可这样又如何呢? 1 sum = 011 2 carry =1000 两者继续异或将会产生 结果就出现了,此时无进位,所以进位为0时,sum将会为最终结果!因为此时不需要进位,异或运算就是最终结果! 优质代码 1 public int getSum(int a,

[leetcode] Sum of Two Integers--用位运算实现加法运算

问题: 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. 分析: 这里要求我们不能用加法.减法等运算符来实现加法运算.这里应该使用位运算来实现加法运算,实际上,这也是计算机CPU内部实现加法运算的方案. x XOR y真值表: x y output 0 0 0 0 1 1

位运算实现加法运算

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. int getSum(int a, int b) { int ans; while(b) { ans = a^b; b = (a&b)<<1; a = ans; } return ans; } 参考: http://

位运算计算加法

int add(int a, int b) { int wei = 0; int jinwei = 0; do { wei = a^b;//处理位加法 jinwei = (a&b) << 1; a = wei; b = jinwei; } while (b != 0); return a; } 两个数相加=两个数亦或(相当于不考虑进位之和)+与左移一位(加上进位的和):

LeetCode之371. Sum of Two Integers

---------------------------------- 使用位运算实现加法: a^b 加不同部分 (a&b)<<1 加相同部分 递归相加 AC代码: public class Solution { public int getSum(int a, int b) { if(b==0) return a; int t1=a^b; int t2=(a&b)<<1; return getSum(t1,t2); } } 题目来源: https://leetcod

位运算的骚操作

位运算的骚操作(一)之四则运算 ? 可以这样说,位运算是我们刚开始学计算机就会接触到的一种东西.那么位运算这么常见,我们是否可以使用它来做一些骚操作呢? 使用的运算符包括下面(java还有一个>>>无符号右移): 含义 运算符 例子 左移(后面补0) << 0011 => 0110 右移(正数前面补0,负数补1) >> 0110 => 0011 按位或 ︳ 0011 ------- => 1011 1011 按位与 & 0011 ----

【leetcode74】Sum of Two Integers(不用+,-求两数之和)

题目描述: 不用+,-求两个数的和 原文描述: 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. 方法一:用位运算模拟加法 思路1: 异或又被称其为"模2加法" 设置变量recipe模拟进位数字,模拟加法的实现过程 代码: public class Solutio

通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)

昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers 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. 因为之前完全没有在实际练习中使用过位运算,所以刚看到这道题目的时候我的第一反应是 1.用乘除代替加减,但是一想,

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 -. 思路:两个数的加法分为两步,对应位相加和进位. 举个简单的例子:997+24 我们平时计算时是将对应位相加和进位同时计算,其实可以保留下进位,只计算对应位相加,保留进位的位置(值).接下来,将进位向左移动一位,将上一步的结果与移位后的进位值进行对应位相加,直到没有进位结束. 对于二进制数的而言,对应