题目是: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. Egret 事件机制

    主要流程: private createGameScene():void { var JimGreen = new Boy(); var HanMeimei = new Girl(); JimGree ...

  2. 上海招聘FPGA讲师(专兼职均可)

    上海招聘FPGA讲师(专兼职均可) 招聘FPGA培训讲师(专兼职均可) 诚聘FPGA培训讲师_软件无线电方向 ◆精通FPGA设计方法及技巧 ◆精通无线电知识及System View设计方法,有基于Sy ...

  3. beini系列_1_U盘引导制作

    奶瓶(beini)这个系统,是一款基于 Tiny Core Linux 搭建的无线网络安全测试系统,当然由于它是用来安全测试的系统,因此在安全方面自然有着强大的功能.而且,这个系统非常简便易学,因此现 ...

  4. poj 3308 Paratroopers

    http://poj.org/problem?id=3308 #include <cstdio> #include <cstring> #include <algorit ...

  5. 新版的DEV RichEdit很强悍,兼容docx,排版更强

    RV至少rtf格式不用自己搞了 Rv没Dev出的强悍 RV最蛋疼的就是表格 DEV目前看来,表格比RV强其他方面来说,觉得到差不多,无所谓dev的excel我整过一次,BUG不少dxRichEdit换 ...

  6. poj2240 - Arbitrage(汇率问题,floyd)

    题目大意: 给你一个汇率图, 让你判断能否根据汇率盈利 #include <iostream> #include <cstdlib> #include <cstdio&g ...

  7. Windows 10 代理上网用户的正确使用姿势

      1.找不到IE,如何使用IE来配置局域网代理 打开Edge浏览器,点击选项,找到“使用Internet Explorer打开” 接下来可以使用熟练的姿势设置IE局域网代理上网了 2.Windows ...

  8. c++ map 插入数据后,begin(),end()以及当前迭代器的变化

    1. map.end()指向map的最后一个元素之后的地址,无论执行map.erase(iter)还是map.add(key, value),map.end()所返回的值永远不会发生变化,都是指向同一 ...

  9. 怎样把.git版本控制文件夹放在项目目录下

    在上传本地代码到本地git库时,.git的存放目录不能放到项目根目录下(报错). 可以先把.git仓库选择其他保存路径,然后再copy到项目根目录下.

  10. MySQL定时事件

    1.创建一个测试表 CREATE TABLE aaa (timeline TIMESTAMP); 2.创建一个事件:每秒插入一条记录到数据表 CREATE EVENT e_test_insert SE ...