371. Sum of Two Integers

My Submissions
Difficulty: Easy
Contributors: Admin

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.

Hide Company Tags
Hulu
Hide Tags
Bit Manipulation
Hide Similar Problems
(M) Add Two Numbers

public class Solution {
    // https://discuss.leetcode.com/topic/49771/java-simple-easy-understand-solution-with-explanation/2
    /*
    "&" AND operation, for example, 2 (0010) & 7 (0111) => 2 (0010)
    
    "^" XOR operation, for example, 2 (0010) ^ 7 (0111) => 5 (0101)
    
    "~" NOT operation, for example, ~2(0010) => -3 (1101) what??? Don't get frustrated here. It's called two's complement.
        1111 is -1, in two's complement
        1110 is -2, which is ~2 + 1, ~0010 => 1101, 1101 + 1 = 1110 => 2
        1101 is -3, which is ~3 + 1
        so if you want to get a negative number, you can simply do ~x + 1
        
    Reference:

    https://en.wikipedia.org/wiki/Two%27s_complement
    
    https://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html
    
    For this, problem, for example, we have a = 1, b = 3,
    
    In bit representation, a = 0001, b = 0011,
    
    First, we can use "and"("&") operation between a and b to find a carry.
    
    carry = a & b, then carry = 0001
    
    Second, we can use "xor" ("^") operation between a and b to find the different bit, and assign it to a,
    
    Then, we shift carry one position left and assign it to b, b = 0010.
    */
    public int getSum(int a, int b) {
      if (a == 0) return b;
      if (b == 0) return a;
      
      while (b != 0) {
          int carry = a & b ;
          a = a ^ b;
          b = carry << 1;
      }
      return a;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市牺丙,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌橘洞,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,198評論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件可岂,死亡現(xiàn)場離奇詭異玖瘸,居然都是意外死亡,警方通過查閱死者的電腦和手機庆猫,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評論 3 398
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來绅络,“玉大人月培,你說我怎么就攤上這事嘁字。” “怎么了杉畜?”我有些...
    開封第一講書人閱讀 167,643評論 0 360
  • 文/不壞的土叔 我叫張陵纪蜒,是天一觀的道長。 經(jīng)常有香客問我此叠,道長纯续,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,495評論 1 296
  • 正文 為了忘掉前任灭袁,我火速辦了婚禮猬错,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘茸歧。我一直安慰自己倦炒,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,502評論 6 397
  • 文/花漫 我一把揭開白布软瞎。 她就那樣靜靜地躺著逢唤,像睡著了一般。 火紅的嫁衣襯著肌膚如雪涤浇。 梳的紋絲不亂的頭發(fā)上智玻,一...
    開封第一講書人閱讀 52,156評論 1 308
  • 那天,我揣著相機與錄音芙代,去河邊找鬼。 笑死盖彭,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播源梭,決...
    沈念sama閱讀 40,743評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼吉挣,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了隧熙?” 一聲冷哼從身側(cè)響起片挂,我...
    開封第一講書人閱讀 39,659評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎贞盯,沒想到半個月后音念,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,200評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡躏敢,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,282評論 3 340
  • 正文 我和宋清朗相戀三年闷愤,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片件余。...
    茶點故事閱讀 40,424評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡讥脐,死狀恐怖遭居,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情旬渠,我是刑警寧澤俱萍,帶...
    沈念sama閱讀 36,107評論 5 349
  • 正文 年R本政府宣布,位于F島的核電站告丢,受9級特大地震影響枪蘑,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜芋齿,卻給世界環(huán)境...
    茶點故事閱讀 41,789評論 3 333
  • 文/蒙蒙 一腥寇、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧觅捆,春花似錦赦役、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至赢赊,卻和暖如春乙漓,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背释移。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評論 1 271
  • 我被黑心中介騙來泰國打工叭披, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人玩讳。 一個月前我還...
    沈念sama閱讀 48,798評論 3 376
  • 正文 我出身青樓涩蜘,卻偏偏與公主長得像,于是被迫代替她去往敵國和親熏纯。 傳聞我的和親對象是個殘疾皇子同诫,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,435評論 2 359

推薦閱讀更多精彩內(nèi)容