[Solved] Adding Binary number in java


dec1 = decimal number representation of num1(binary)
k = binary factor
dec1 = dec1 + (num1%10) * k;

Here he is building the decimal number from binary number.

(num1%10) gives the last digit of the number.
Ex: num1 = 110

First iteration:

    dec1 = 0 +(110 %10) *1
    dec1 = 0
    k = 1*2

Second iteration:

dec1 = 0 +(11%10) * 2
dec1 = 2
k = 2*2

Third iteration:

dec1 = 2+ (1%10) *4
dec1 = 6

Finally the dec representation of 110 is 6

1

solved Adding Binary number in java