码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-01-11 07:56:37      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:creating   tps   spec   height   bsp   int   discuss   this   https   

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






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

标签:creating   tps   spec   height   bsp   int   discuss   this   https   

原文地址:http://www.cnblogs.com/xiejunzhao/p/2a2e6114090553da472fb3e2c48f2904.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!