Java Program to Convert Binary Number to Decimal and vice-versa
What is a Binary Number?
A binary number is a number represented by a base-2 numeral system, which uses only two symbols: 0 and 1. Each digit, or bit, in a binary number represents a power of 2, with the rightmost bit representing 2^0 and the next bit to the left representing 2^1, and so on.
Convert a binary number to decimal :
- Start with the rightmost bit of the binary number and assign it the value 2^0.
- For each bit moving left, double the previous value and add the value of the current bit.
Here is an example of how to convert the binary number 1101 to decimal:
- The rightmost bit is 1, so 2^0 * 1 = 1.
- The next bit is 1, so 2^1 * 1 = 2.
- The next bit is 0, so 2^2 * 0 = 0.
- The leftmost bit is 1, so 2^3 * 1 = 8.
- Add up all the values (1 + 2 + 0 + 8) to get 11, which is the decimal equivalent of the binary number 1101.
Example 1:
public class Main { public static void main(String[] args) { String binary = "1101"; int decimal = binaryToDecimal(binary); System.out.println("Decimal equivalent of binary number " + binary + " is: " + decimal); } public static int binaryToDecimal(String binary) { int decimal = 0; int binaryLen = binary.length(); for (int i = 0; i < binaryLen; i++) { int bit = binary.charAt(i) - '0'; int power = binaryLen - i - 1; decimal += bit * Math.pow(2, power); } return decimal; } }
Output :
Decimal equivalent of binary number 1101 is: 13
Explanation :
The binaryToDecimal() method uses a for loop to iterate through each character of the binary number, starting from the rightmost character. For each character, it gets the integer value of the character by subtracting '0' (the ASCII value of '0') from the character. This gives the value of the bit at that position (0 or 1). Then it calculates the power of 2 that the bit represents by subtracting the index of the current character from the length of the binary number and subtracting 1. Finally, it adds the value of the bit multiplied by the power of 2 to the total decimal value.
Convert decimal to binary :
- Divide the decimal number by 2.
- Write down the remainder.
- Repeat step 1 and 2 until the quotient is 0.
- Read the remainders from bottom to top.
Here is an example of how to convert the decimal number 13 to binary using the algorithm :
- Divide 13 by 2: quotient = 6, remainder = 1.
- Divide 6 by 2: quotient = 3, remainder = 0.
- Divide 3 by 2: quotient = 1, remainder = 1.
- Divide 1 by 2: quotient = 0, remainder = 1.
- Read the remainders from bottom to top: 1101.
Example 2:
public class Main { public static void main(String[] args) { int decimal = 13; String binary = decimalToBinary(decimal); System.out.println("Binary equivalent of decimal number " + decimal + " is: " + binary); } public static String decimalToBinary(int decimal) { String binary = ""; while (decimal > 0) { binary = (decimal % 2) + binary; decimal = decimal / 2; } return binary; } }
Output :
Binary equivalent of decimal number 13 is: 1101
Explanation :
The decimalToBinary() method uses a while loop to repeatedly divide the decimal number by 2 and get the remainder until the quotient becomes 0. It concatenates each remainder to a string variable "binary" and returns it. The remainders are read from bottom to top, so the last remainder is the leftmost digit of the binary number and the first remainder is the rightmost digit.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment