Adding binary numbers

后端 未结 21 1131
孤独总比滥情好
孤独总比滥情好 2020-11-28 07:50

Does anyone know how to add 2 binary numbers, entered as binary, in Java?

For example, 1010 + 10 = 1100.

相关标签:
21条回答
  • 2020-11-28 08:24
    package Assignment19thDec;
    
    import java.util.Scanner;
    
    public class addTwoBinaryNumbers {
    
        private static Scanner sc;
    
        public static void main(String[] args) {
    
            sc = new Scanner(System.in);
            System.out.println("Enter 1st Binary Number");
            int number1=sc.nextInt();
            int reminder1=0;
            int number2=sc.nextInt();
            int reminder2=0;
            int carry=0;
            double sumResult=0 ;int add = 0
            ;
            int n;
            int power=0;
            while (number1>0 || number2>0) {
    
                /*System.out.println(number1 + " " +number2);*/
    
                reminder1=number1%10;
                number1=number1/10;
                reminder2=number2%10;
                number2=number2/10;
                /*System.out.println(reminder1 +"  "+ reminder2);*/
    
    
                if(reminder1>1 || reminder2>1 ) {
                    System.out.println("not a binary number");
                    System.exit(0);
                }
    
                n=reminder1+reminder2+carry;
                switch(n) {
    
                case 0: 
                        add=0; carry=0;                                 
                        break;                  
                case 1: add=1; carry=0;
                        break;
                case 2: add=0; carry=1;
                        break;
                case 3: add=1;carry=1;
                        break;
                default: System.out.println("not a binary number ");
    
                }
    
                sumResult=add*(Math.pow(10, power))+sumResult;
                power++;
    
    
            }
    
            sumResult=carry*(Math.pow(10, power))+sumResult;
            System.out.println("\n"+(int)sumResult);
    
    
        }
    
    }
    
    0 讨论(0)
  • 2020-11-28 08:31

    Use Integer.parseInt(String, int radix).

     public static String addBinary(){
     // The two input Strings, containing the binary representation of the two values:
        String input0 = "1010";
        String input1 = "10";
    
        // Use as radix 2 because it's binary    
        int number0 = Integer.parseInt(input0, 2);
        int number1 = Integer.parseInt(input1, 2);
    
        int sum = number0 + number1;
        return Integer.toBinaryString(sum); //returns the answer as a binary value;
    }
    
    0 讨论(0)
  • 2020-11-28 08:31
    import java.util.Scanner;
    
    {
        public static void main(String[] args) 
        {
            String b1,b2;
            Scanner sc= new Scanner(System.in);
            System.out.println("Enter 1st binary no. : ") ;
            b1=sc.next();
            System.out.println("Enter 2nd binary no. : ") ;
            b2=sc.next();
            int num1=Integer.parseInt(b1,2);
            int num2=Integer.parseInt(b2,2);
            int sum=num1+num2;
            System.out.println("Additon is : "+Integer.toBinaryString(sum));
        }
    
    }
    
    
    0 讨论(0)
提交回复
热议问题