题目是:Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.

思路:两个数的加法分为两步,对应位相加和进位。

举个简单的例子:997+24

我们平时计算时是将对应位相加和进位同时计算,其实可以保留下进位,只计算对应位相加,保留进位的位置(值)。接下来,将进位向左移动一位,将上一步的结果与移位后的进位值进行对应位相加,直到没有进位结束。

对于二进制数的而言,对应位相加就可以使用异或(xor)操作,计算进位就可以使用与(and)操作,在下一步进行对应位相加前,对进位数使用移位操作(<<)。

这样就非常好理解下面的实现代码。

 int getSum(int a, int b)
{
while (b)
{
int c = a ^ b;
b = (a & b) << ;
a = c;
}
return a;
}

最后,再给一个详细的运行过程示意,计算523+1125.(另外,如果是有负数的话,算法也是可行的,可以去看一下补码的相关内容)

Leetcode 371: Sum of Two Integers(使用位运算实现)的更多相关文章

  1. 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)

    剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...

  2. 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 ...

  3. LeetCode: 371 Sum of Two Integers(easy)

    题目: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. ...

  4. 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 ...

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

    昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers Calculate the sum of two integers a and b, b ...

  6. 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) { ...

  7. 【一天一道LeetCode】#371. Sum of Two Integers

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Calcula ...

  8. 【LeetCode】371. Sum of Two Integers 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位运算 日期 题目地址:https://leetco ...

  9. [LeetCode&Python] Problem 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 ...

随机推荐

  1. Java进阶代码

    本文重在温习……不过初学以及进阶高手不可错过 1.  public static void arraycopy(全小写)(object src,int srcPos,object dest,int d ...

  2. Android ANR、Force Closed(转)

    ANRs (“Application Not Responding”),意思是”应用没有响应“. 在如下情况下,Android会报出ANR错误: – 主线程 (“事件处理线程” / “UI线程”) 在 ...

  3. 【HDOJ】5063 Operation the Sequence

    #include <cstdio> #include <cstring> #include <cstdlib> #define MAXN 100005 #defin ...

  4. POJ2996 Help Me with the Game(模拟)

    题目链接. 分析: 简单模拟. #include <iostream> #include <queue> #include <cstdio> #include &l ...

  5. NOI 2005 维修数列

    妈妈呀我终于过了!!!原来是数据坑我!!! 弃疗弃疗弃疗弃疗!!!!我调了一天呢....被GET_SUM 8 0打败了.... 啥也不说了....还是我太年轻.... 更新了一下常数,跑的还是可以的: ...

  6. 【转】android去掉EditView的默认焦点问题

    原文网址:http://www.111cn.net/sj/android/54680.htm 做一个输入框时发现android中EditView的默认焦点了,这种问题如果是在输入框还好,但在搜索页面或 ...

  7. bzoj2208 [Jsoi2010]连通数(scc+bitset)

    2208: [Jsoi2010]连通数 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1879  Solved: 778[Submit][Status ...

  8. (1.1.9)UVA 10930 A-Sequence(模拟)

    /* * UVA_10930_1.cpp * * Created on: 2013年10月7日 * Author: Administrator */ #include <iostream> ...

  9. windows 编程—— 常用函数 与 操作

    目录: MessageBox() 和 PlaySound() 获得窗口 或屏幕大小 获得字体大小 输出文字 屏蔽和显示控制台窗口 1. MessageBox() 和 PlaySound() Messa ...

  10. idea 15 搭建maven web项目

    加上这个创建会比较快