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

我们平时计算时是将对应位相加和进位同时计算,其实可以保留下进位,只计算对应位相加,保留进位的位置(值)。接下来,将进位向左移动一位,将上一步的结果与移位后的进位值进行对应位相加,直到没有进位结束。
对于二进制数的而言,对应位相加就可以使用异或(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(使用位运算实现)的更多相关文章
- 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)
剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...
- 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: 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 -. ...
- 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:371. Sum of Two Integers)
昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers Calculate the sum of two integers a and b, b ...
- 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 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Calcula ...
- 【LeetCode】371. Sum of Two Integers 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位运算 日期 题目地址:https://leetco ...
- [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 ...
随机推荐
- IOS 笔试
iOS基础教程之Objective-C:Objective-C笔试题 作者:蓝鸥科技 发布于:2012-12-14 14:38 Friday 分类:iOS中级-OC开发 iOS基础教程之Objecti ...
- mac 下 配置 阿帕奇
1.从 tomcat 官网(http://tomcat.apache.org/download-90.cgi)下载 完整的 tomcat包. 2.将红框中的包下载完,然后解压到任意一个目录,将其命名为 ...
- Weblogic11g+Axis1.4 实现WebService服务
IDE:NetBeans8.0 项目结构: (1)新建接口Hello.java package com.test; /** * @author y * @date 2015-9-5 7:51:29 * ...
- CENTOS纯手工安装LAMP+PHPMYADMIN
现在,安装这些确实越来越方便了. Installing Apache2 With PHP5 And MySQL Support On CentOS 6.4 (LAMP) 参考URL: http://w ...
- PBOC规范研究
一.ISO14443协议和PBOC关于CID的约定 看过协议的人其实都明白,RATS命令中参数字节的低半字节是CID,期中,CID不能为15. ISO14443协议中要求当RATS命令的CID等于0时 ...
- Delphi 缩放图像代码 - 支持PNG透明通道(利用了Windows的windowscodecs.dll)
要求Delphi2007或者更高版本, 系统要求至少XP-SP2以上 实际上是利用了Windows的windowscodecs.dll这个文件的功能 在VCL里已经封装为TWICImage类 proc ...
- 【动态规划】XMU 1032 装配线问题
题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1032 题目大意: 一个物品在2条生产线上加工,每条线上n(n<=1000)个节点 ...
- Win7无法访问NAS或Samba服务器解决之道 转
Win7无法访问NAS或Samba服务器解决之道 http://www.sina.com.cn 2010年05月12日 01:41 IT168.com [IT168 应用]默认情况下,Window ...
- 【5】JAVA---地址App小软件(DeletePanel.class)(表现层)
删除地址的表现层类. 如果没有选中要删除的地址信息,会出现窗口提示: 删除地址界面:(无法修改数据,只能看) /* * DeletePanel.java * */ package cn.hncu.ad ...
- 酷派D530刷机指引之官方ROM
刷机前的准备工作 刷官方ROM的大致过程就是:先手机连接电脑,然后在电脑上运行刷机工具,然后那个刷机工具就会把你选择的ROM装到手机里面,然后就没有然后了. 所以在刷机之前,硬件方面需要准备好: 充满 ...