实现二进制数相加

import java.util.Scanner;

public class Solution{
	public static void main(String args[]){
			Solution solution = new Solution();
			System.out.print("please input the first binary String:");
			String a = new Scanner(System.in).next().toString();
			
			System.out.print("please input the second binary String:");
			String b = new Scanner(System.in).next().toString();
			
			int sum = solution.bTos(a) + solution.bTos(b);
			System.out.print("the sum is :"+Integer.toBinaryString(sum));
			
	}
	public int bTos(String s){
		int x = 0;
		for(char c:s.toCharArray())
		x = x*2 + (c == '1'?1:0);
		return x;
	}
} 

  

posted @ 2015-04-07 14:33  一维世界  阅读(265)  评论(0编辑  收藏  举报