题目描述
写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。
java
public class Solution { public int Add(int num1,int num2) { while (num2!=0) { int temp = num1^num2; num2 = (num1&num2)<<1; num1 = temp; } return num1; } }
python
1 class Solution: 2 def Add(self, num1, num2): 3 # write code here 4 while num2!=0: 5 sum_ = num1 ^ num2 6 carry = (num1 & num2)<<1 7 num1=sum_%0x100000000 8 num2=carry%0x100000000 9 if num1<=0x7FFFFFFF: 10 return num1 11 else: 12 return num1 |(~0x100000000+1)
原文地址:https://www.cnblogs.com/NPC-assange/p/12109188.html
时间: 2024-10-24 13:01:33