We already know what are the bits, bytes and binary values. And we also know how to convert binary numbers to decimal numbers and decimal numbers to binary numbers. Let's write a program to convert user entered binary number into decimal number and decimal number into binary number.
Convert Binary Number To Decimal Number
Here is the example for the conversion from binary number to decimal number -
1 | 0 | 1 | 0 | 0 | 1 | 0 | 1 |
---|---|---|---|---|---|---|---|
1 x 27 | 0 x 26 | 1 x 25 | 0 x 24 | 0 x 23 | 1 x 22 | 0 x 21 | 1 x 20 |
128 | 0 | 32 | 0 | 0 | 4 | 0 | 1 |
Decimal Value for 1010 0101 is = 128 + 32 + 4 + 1 = 165.
Let's establish the logic behind this conversion.
- First, we need to have binary number as a long data type to have enough bits in the binary value. Long binaryVal = 10100101
- The program will read every bit of the binary value from right to left in while loop. We can get each bit by using the remainder operator (binaryVal % 10) and convert it to integer as the data type is Long.
- The remainder will be multiplied by 2 to the power of bit position from right to left. The product will be added to the old product.
- New binary value will be (binaryVal / 10) and go back to while loop. Loop will terminate when binaryVal is 0.
Loop Logic -
10100101 % 10 => 1 => 1x20 => 1 and 10100101 / 10 => 1010010
1010010 % 10 => 0 => 0x21 => 0 and 1010010 / 10 => 101001
101001 % 10 => 1 => 1x22 => 4 and 101001 / 10 => 10100
10100 % 10 => 0 => 0x23 => 0 and 10100 / 10 => 1010
1010 % 10 => 0 => 0x24 => 0 and 1010 / 10 => 101
101 % 10 => 1 => 1x25 => 32 and 101 / 10 => 10
10 % 10 => 0 => 0x26 => 0 and 10 / 10 => 1
1 % 10 => 1 => 1x27 => 128 and 1 / 10 => 0
1 + 0 + 4 + 0 + 0 + 32 + 0 + 128 = 165
Here is the program for conversion binary number to decimal number. The program has an extra method to calculate the power of number. Java has a built-in method Java.lang.Math.pow() but only for parameters as double data type. So, I wrote a method for int data type - int calcPower(int no1, int no2)
import java.util.Scanner;
public class BinaryToDecimal {
public static void main(String args[]) {
System.out.println("Enter the binary value");
Scanner sc = new Scanner(System.in);
Long binaryVal = sc.nextLong();
int decVal = 0;
int rem, pow = 0;
Long origBinaryVal = binaryVal;
while(binaryVal != 0) {
rem = (int)(binaryVal % 10);
decVal = decVal + rem * calcPower(2, pow);
binaryVal = binaryVal / 10;
pow++;
}
System.out.println("Binary Value: " + origBinaryVal);
System.out.println("Decimal Value: " + decVal);
}
// Calculates no1 To The Power no2
static int calcPower(int no1, int no2) {
int powVal = 1;
for(int i = 0; i < no2; i++) {
powVal = powVal * no1;
}
return powVal;
}
}
And the output is:
Enter the binary value
10100101
Binary Value: 10100101
Decimal Value: 165
Convert Decimal Number To Binary Number
Here is the example for the conversion from decimal number to binary number -
Divide by 2 | Quotient | Remainder | 2 to the power |
---|---|---|---|
165 / 2 | 82 | 1 | 20 |
82 / 2 | 41 | 0 | 21 |
41 / 2 | 20 | 1 | 22 |
20 / 2 | 10 | 0 | 23 |
10 / 2 | 5 | 0 | 24 |
5 / 2 | 2 | 1 | 25 |
2 / 2 | 1 | 0 | 26 |
1 / 2 | 0 | 1 | 27 |
Binary Number for 165 is = 10100101
Let's establish the logic behind this conversion.
- Divide decimal value by 2 and get quotient and remainder values. Multiply the remainder by 10 to the power of bit's position. Result is the leftmost bit for the binary number. New decimal value is equal to quotient.
- Repeat step 1 for new decimal value. Add the result to the earlier result.
- Repeat the steps till decimal value is 0. The total result is the converted binary number.
Loop Logic -
165 % 2 => 1 => 1x100 => 1 and 165 / 2 => 82
82 % 2 => 0 => 0x101 => 0 and 82 / 2 => 41
41 % 2 => 1 => 1x102 => 100 and 41 / 2 => 20
20 % 2 => 0 => 0x103 => 0 and 20 / 2 => 10
10 % 2 => 0 => 0x104 => 0 and 10 / 2 => 5
5 % 2 => 1 => 1x105 => 100000 and 5 / 2 => 2
2 % 2 => 0 => 0x106 => 0 and 2 / 2 => 1
1 % 2 => 1 => 1x107 => 10000000 and 1 / 2 => 0
1 + 0 + 100 + 0 + 0 + 100000 + 0 + 10000000 = 10100101
Here is the program for conversion binary number to decimal number.
import java.util.Scanner;
public class DecimalToBinary {
public static void main(String args[]) {
System.out.println("Enter the decimal value");
Scanner sc = new Scanner(System.in);
int decVal = sc.nextInt();
Long binaryVal = 0L;
int rem, pow = 0;
int origDecVal = decVal;
while(decVal != 0) {
rem = (int)(decVal % 2);
decVal = decVal / 2;
binaryVal = binaryVal + rem * calcPower(10, pow);
pow++;
}
System.out.println("Decimal Value: " + origDecVal);
System.out.println("Binary Value: " + binaryVal);
}
// Calculates no1 To The Power no2
static int calcPower(int no1, int no2) {
int powVal = 1;
for(int i = 0; i < no2; i++) {
powVal = powVal * no1;
}
return powVal;
}
}
And the output is:
Enter the decimal value
165
Decimal Value: 165
Binary Value: 10100101
Code can be accessed here: Github Link
No comments:
Post a Comment