Binary to Decimal Conversion
It is a number system conversion from binary to decimal number system that is carried out by arithmetic operation addition and a power function.
Binary to Decimal conversion - steps
Given binary 8 bits, converts it into decimal value
- set decval==0
- Read a LSB bit from binary8bits
- IF LSB==1
- add decval + 2 power of the LSB position value
- End IF
- Next LSB
- print decval
binary to decimal value conversion - Example
Given binary 8 bits = 10001111 ,convert it into a decimal value. The position value(0 to n-1 bits) starts right to left on the binary bits. 1000 1 1 1 1 | | | | |-----1 x 2^0 = 1 | | | |---------1 x 2^1 =2 | | |-------------1 x 2^2 =4 | | ----------------1 x 2^3 =8 | | |--------------------- 1 x 2^7 =128 decimal value = 1 + 2 + 4 + 8 + 128 = 143 binary 8 bits 10001111 = decimal value 143.
Binary to Decimal conversion - Java programming code
The Java program converts binary 8 bits numbers into decimal value using arithmetic addition operator and power function. while the program is running, it waits for user to enter binary bits and returns a decimal value as result of the given binary bits.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Binary2Decimal {
public static void main(String[] args) throws IOException {
InputStreamReader dis =new InputStreamReader ( System.in );
BufferedReader br =new BufferedReader(dis);
System.out.println("Binary 8 bits to Decimal Conversion \n");
System.out.println("Enter binary bits");
String inpstr = br.readLine();
int decval =0;
StringBuffer bufstr = new StringBuffer(inpstr);
bufstr.reverse();
for(int n=0; n<bufstr.length() ; n++ )
{
if ( bufstr.charAt(n) == '1' ) {
decval += Math.pow(2,n);
}
}
System.out.println("\nDecimal value :" + decval);
}
}
Binary to Decimal converion - Java programming Output
The program
Binary 8 bits to Decimal Conversion Enter binary 8 bits : 01010101 Decimal value :85
Comments
Post a Comment