`

Sum of Two Integers——Bit Manipulation

 
阅读更多

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.

 

class Solution(object):
    def getSum(self, a, b):
        """
        :type a: int
        :type b: int
        :rtype: int
        """
        # 32 bits integer max
        MAX = 0x7FFFFFFF
        # 32 bits interger min
        MIN = 0x80000000
        # mask to get last 32 bits
        mask = 0xFFFFFFFF
        while b != 0:
            # ^ get different bits and & gets double 1s, << moves carry
            a, b = (a ^ b) & mask, ((a & b) << 1) & mask
        # if a is negative, get a's 32 bits complement positive first
        # then get 32-bit positive's Python complement negative
        return a if a <= MAX else ~(a ^ mask)

 

分享到:
评论

相关推荐

    程序设计题目

    Calculate A + B. Each line will contain two integers A and B. Process to end of file. For each case, output A + B in one line.

    A.Collection.of.Bit.Programming.Interview.Questions.solved.in.C++

    Given an array of integers where all the numbers are appearing twice find the only two numbers which appears once Chapter 12. Multiply two numbers without using arithmetic operators Chapter 13. ...

    大数处理问题(就是用字符串)

    Given two integers A and B, your job is to calculate the Sum of A + B. Input The first line of the input contains an integer T(1 ) which means the number of test cases. Then T lines follow, each ...

    Given an array of integers, return indices of the two numbers

    Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same ...

    杭电ACM 1002

    Given two integers A and B, your job is to calculate the Sum of A + B. Input The first line of the input contains an integer T(1) which means the number of test cases. Then T lines follow, each ...

    Two Sum leetcode c++

    Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where...

    a_plus_b.cpp

    输入 Input two integers A and B, process to the end of the file. (Watch the Sample Input) 输出 For each case, output A + B in one line.(Watch the Sample Output)

    怎么采用C语言的程序编写的过程实现序列的删除

    The first line contains two integers n and m (1≤n≤2000,0≤m≤2000) -- the number of vertices and the number of edges. For the next m lines, each line contains two integers ui and vi, which means ...

    An Exposition of the Eisenstein Integers

    An Exposition of the Eisenstein Integers An Exposition of the Eisenstein Integers

    嵌入式虚拟机CarpVM.zip

    ADD (): Pops the top two integers from the stack and pushes their sum. SUB (): Pops the top two integers from the stack and pushes the difference (lower minus upper). MUL (): Pops the top two ...

    acm练习题大数计算,运用ASCII码解决进位问题.c

    Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the ...

    sum it up !

    Given a specified total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. For example, if t=4, n=6, and the list is [4,3,2,2,1,1], then there are four ...

    even-Product-Sum.zip_SUM

    R code. There are two functions: evenProduct(num) gives product of all even positive integers less then or equal to ...evenProduct(num) gives sum of all even positive integers less then or equal to num

    C++大学教程(第七版)C++ How to Program, Seventh Edition

    // Addition program that displays the sum of two integers. #include &lt;iostream&gt; // allows program to perform input and output // function main begins program execution int main() { // variable ...

    SubSetSum_return_求最小子集和_

    Use an approximate algorithm to find the sum of subsets. S is a set of positive integers. Find a subset of S whose sum should be as large as possible but cannot exceed t

    LeetCode——001 Two Sum

    Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same ...

    SIMD Compression and the Intersection of Sorted Integers (simdcompressionarxiv)-计算机科学

    Verse Communications84 La EspiralOrinda, CA 94563 USAnate@verse.comABSTRACTSorted lists of integers are commonly used in inverted in- dexes and database systems. They are often compres

    The irrationals: a story of the numbers you can't count on

    That definition seems so simple: they are numbers that cannot be expressed as a ratio of two integers, or that have decimal expansions that are neither infinite nor recurring. But, as The Irrationals...

    remotejvm:该库将提供一种简单地在多个节点上分发Java应用程序的方法

    在多个JVM上分布Java应用程序该库...用法这是一个简单的例子public class Demo { static public interface Worker extends Serializable { // Just compute the sum of two integers public Integer add ( Integer a ,

Global site tag (gtag.js) - Google Analytics