首页 > 其他 > 详细

实现二进制数相加

时间:2015-04-07 15:30:05      阅读:213      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!