标签:
JavaScript实现
function add(num1,num2){ var sum1=num1^num2;//忽略进位,异或 var sum2=(num1&num2)<<1;//与,移位,进位运算 sum=sum1+sum2;//相加 return sum; } console.log(add(12,18));//30
Java实现,控制台输入数据
1 import java.util.Scanner; 2 3 4 public class add { 5 static int sum=0; 6 public static int test(int a,int b){ 7 int sum1=a^b; 8 int sum2=(a&b)<<1; 9 sum=sum1+sum2; 10 return sum; 11 } 12 13 public static void main(String[] args){ 14 Scanner con=new Scanner(System.in); 15 int a=con.nextInt(); 16 int b=con.nextInt(); 17 18 System.out.print(test(a,b)); 19 } 20 }
标签:
原文地址:http://www.cnblogs.com/dll-ft/p/5861324.html