We already discussed about the binary numbers and also how to convert the binary numbers to the decimal numbers and convert the decimal numbers to the binary numbers. Let's talk about how to add binary numbers.
We know that binary numbers have only 0s and 1s, and when we add two or more binary numbers, the result should be same as the result of their respective decimal numbers.
How bits are added
0 | 0 | 1 | |||||
+ | 0 | + | 1 | + | 1 | ||
0 | 1 | 1 | 0 |
0 + 0 = 0 (0)
0 + 1 = 1 (1)
1 + 1 = 2 (10)
And when there are more bits, the binary addition will look like this -
When two 1s are added, result is 10 and 1 is carried over to left.
1 | <- | |||||||||
1 | 0 | 1 | 0 | 1 | 1 | |||||
+ | 0 | + | 1 | + | 1 | |||||
1 | 0 | 1 | 1 | 1 | 0 | 0 |
2 + 0 = 2 (10)
2 + 1 = 3 (11)
3 + 1 = 4 (100)
Let's try to add two binary numbers - 28 (11100) and 43 (101011) and result should be 71 (1000111)
28 + 43 = 71
1 | 1 | <- | ||||
1 | 0 | 1 | 0 | 1 | 1 | |
+ | 1 | 1 | 1 | 0 | 0 | |
1 | 0 | 0 | 0 | 1 | 1 | 1 |
Now, let's write a program for binary addition - I have used two methods binaryToDecimal() and calcPower() same as in the last post convert the binary numbers to the decimal numbers and convert the decimal numbers to the binary numbers.
public static int binaryToDecimal(Long binaryVal)
public static int calcPower(int no1, int no2)
And also added one more method to add two binary numbers - addBinaryNumbers()
public static Long addBinaryNumbers(Long binary1, Long binary2)
I have used the same logic as explained above -
- The program will read both the binary numbers bit by bit from right to left in a while loop, till all bits are done.
- Add both bits, and also add carryover bit if there is any.
- Add the result bit to convert result into Long binary number again.
- If in last, there is carryover left, add that to the result too.
import java.util.Scanner;
public class AddBinaryNumbers {
public static void main(String args[]) {
System.out.println("Enter two binary values");
Scanner sc = new Scanner(System.in);
Long binary1 = sc.nextLong();
Long binary2 = sc.nextLong();
addBinaryNumbers(binary1, binary2);
}
public static int binaryToDecimal(Long binaryVal) {
int decVal = 0;
int rem, pow = 0;
while(binaryVal != 0) {
rem = (int)(binaryVal % 10);
decVal = decVal + rem * calcPower(2, pow);
binaryVal = binaryVal / 10;
pow++;
}
return decVal;
}
public static int calcPower(int no1, int no2) {
int powVal = 1;
for(int i = 0; i < no2; i++) {
powVal = powVal * no1;
}
return powVal;
}
public static Long addBinaryNumbers(Long binary1, Long binary2) {
Long binaryVal = 0L;
int sum = 0;
int rem1, rem2, remainder = 0, i = 0, pow = 0;
int decVal1 = binaryToDecimal(binary1);
int decVal2 = binaryToDecimal(binary2);
System.out.println("Binary number 1 => " + binary1 + " : " + decVal1);
System.out.println("Binary number 2 => " + binary2 + " : " + decVal2);
// Addition is done bit by bit
while(binary1 != 0 || binary2 != 0) {
// rem1 is next bit of binary1 and rem2 is next bit of binary2
rem1 = (int)(binary1 % 10);
rem2 = (int)(binary2 % 10);
// remaining bits of binary numbers
binary1 = binary1 / 10;
binary2 = binary2 / 10;
// sum of the bits + carryover if any
sum = (rem1 + rem2 + remainder) % 2; // remainder is carry over
remainder = (rem1 + rem2 + remainder) / 2;
binaryVal = binaryVal + sum * calcPower(10, pow);
pow++;
}
if(remainder != 0) {
// If last bit addition has carryover, add it too
binaryVal = binaryVal + calcPower(10, pow);
}
int totVal = binaryToDecimal(binaryVal);
System.out.println("Sum of two binary numbers => " + binaryVal + " : " + totVal);
return binaryVal;
}
}
Here is the output:
Enter two binary values
11100
101011
Binary number 1 => 11100 : 28
Binary number 2 => 101011 : 43
Sum of two binary numbers => 1000111 : 71
Enter two binary values
111111111
111111111
Binary number 1 => 111111111 : 511
Binary number 2 => 111111111 : 511
Sum of two binary numbers => 1111111110 : 1022
NOTE: This example is good for till 9 bits binary numbers as according to Long data type values range.
The code can be accessed here: Github Link
No comments:
Post a Comment