实现二进制数相加
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; } }