1. class Solution {
  2. public:
  3. /*
  4. * @param a: The first integer
  5. * @param b: The second integer
  6. * @return: The sum of a and b
  7. */
  8. int aplusb(int a, int b) {
  9. // write your code here, try to do it without arithmetic operators.
  10. if(a==)return b;
  11. if(b==)return a;
  12. int x1 = a^b;
  13. int x2 = (a&b)<<;
  14. return aplusb(x1,x2);
  15. }
  16. };

俩位相加有四种情况:

1. 1 + 1 = 10;

2. 1 + 0 = 01;

3. 0 + 1 = 01;

4. 0 + 0 = 00;

第一种情况,即有进位,用 (a&b)<<1解决;

第二第三种情况,用a^b解决。

LintCode-A + B 用位操作模拟加法的更多相关文章

  1. qq教xixi写模拟加法【非常爆炸】

    #include<iostream> #include<cstdio> #include<math.h> #include<queue> #includ ...

  2. 为什么位运算可以实现加法(1、 不考虑进位的情况下位运算符中的异或^可以表示+号)(2、 位运算符中的与运算符&和左移运算符<<可以模拟加法中的进位)(3、位运算不仅可以做加法,还可以做其它的乘法减法等:计算机本质是二进制运算)

    为什么位运算可以实现加法(1. 不考虑进位的情况下位运算符中的异或^可以表示+号)(2. 位运算符中的与运算符&和左移运算符<<可以模拟加法中的进位)(3.位运算不仅可以做加法,还 ...

  3. UVa 12333 - Revenge of Fibonacci manweifc(模拟加法竖式 & 字典树)

    题意: 给定n个(n<=40)数字, 求100000个以内有没有前面n个数字符合给定的数字的fibonacci项, 如果有, 给出最小的fibonacci项, 如果没有, 输出-1. 分析: 可 ...

  4. 【LintCode·容易】用栈模拟汉诺塔问题

    用栈模拟汉诺塔问题 描述 在经典的汉诺塔问题中,有 3 个塔和 N 个可用来堆砌成塔的不同大小的盘子.要求盘子必须按照从小到大的顺序从上往下堆 (如:任意一个盘子,其必须堆在比它大的盘子上面).同时, ...

  5. 基本类型算法题目学习(EPI)

    1.关于奇偶校验的方法中,如何快速的求取一个64-bit的数字的奇偶校验位.(如果1的位数为奇数,则奇偶校验位为1,如果1的位数为偶数,则奇偶校验位为0) a.暴力枚举法采用一位一位进行计算,一位一位 ...

  6. hdu 4099 Revenge of Fibonacci Trie树与模拟数位加法

    Revenge of Fibonacci 题意:给定fibonacci数列的前100000项的前n位(n<=40);问你这是fibonacci数列第几项的前缀?如若不在前100000项范围内,输 ...

  7. [leetcode] 位操作题解

    子集 题目[78]:给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 示例: 输入: nums = [1,2,3] 输出: [ [3],   [1],   [2],   [ ...

  8. hdu1753()模拟大型实景数字相加

    标题信息: 手动求大的实数在一起, pid=1753">http://acm.hdu.edu.cn/showproblem.php?pid=1753 AC代码: /**  *大实数相加 ...

  9. Codeforces Round #550 (Div. 3) E. Median String (模拟)

    Median String time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

随机推荐

  1. Deploy a Sharded Cluster

    Start the Config Server Database Instances for example :  mongod --configsvr --dbpath <path> - ...

  2. HDU 5418 Victor and World (Floyd + 状态压缩DP)

    题目大意:从起点 1 开始走遍所有的点,回到起点 1 ,求出所走的最短长度. 思路:首先利用 Floyed 求出任意两点之间的最短距离 dis[i][j].求出任意两点之间的最短距离后,运用动态规划. ...

  3. <转载>Wait and Waitpid

    转载http://www.cnblogs.com/lihaosky/articles/1673341.html 一.Wait #include <sys/types.h> /* 提供类型p ...

  4. js jquery 验证写法

    <?php header("Content-type: text/html; charset=utf-8"); ?> <script src="jque ...

  5. python list 按长度分段

    def changes(a,b): #list 分段函数,a:数据[(1),(2)]b:长度 for i in xrange(0,len(a),b): yield  a[i:i+b] for i in ...

  6. PHP操作MySQL数据库的相关函数

    首先,要分清SQL语句的类型: SQL语句的分类 (1)DDL:Data Define Language,数据定义语言--定义表的列结构 CREATE.DROP.ALTER.TRUNCATE (2)D ...

  7. 2014 BDTC 參会有感

    中国大数据技术大会(Big Data Technology Conference,BDTC)是眼下国内最具影响.规模最大的大数据领域的技术盛会. 大会的前身是Hadoop中国云计算大会(Hadoop ...

  8. mybatis参数查询

    单个参数查询            在mapper.xml配置文件中配置                   <select id= "selectByNu" paramet ...

  9. jQuery validate 的valid()方法一直返回true

    1 调用$('#myForm').valid(),一直返回ture eg:html <form id="myForm"> <input class="f ...

  10. .net对象转Datable

    public static DataTable GetDataTable<T>( IEnumerable<T> list,string tableName) { DataTab ...