码迷,mamicode.com
首页 > 其他好文 > 详细

实现二进制数相加

时间:2015-04-07 15:31:11      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:

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;
	}
} 

  

实现二进制数相加

标签:

原文地址:http://www.cnblogs.com/nero-bupt/p/4398269.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!