Decimal to binary and Binary to decimal
The number system conversion process is carried out between base 10 and base 2 number system. The base 10 number system has 10 distinct numbers from 0 to 9 is called decimal number system and the base 2 number system has 2 distinct numbers 0 and 1 is binary number system.
Decimal to binary (base 10 to base 2) conversion - algorithm
Given a decimal value, decval and converts it into binary bits
- modulus decval by 2, it returns a reminder ( 0 or 1)
- divide decval by 2, it returns a quotient
- the quotient becomes the decval
- and repeat step 1 to 3 until decval becomes 0
- the reminders on each iteration is results of binary bits
Decimal to binary (base 10 to base 2) conversion - Example
Given decimal value = 47 ,convert it into binary bits divide the decimal value by 2 returns a quotient and reminder, the returned quotient becomes decimal value for next division by 2 divider | quotient | reminder 2 | 47 ----------- 2 | 23 | 1 ----------- 2 | 11 | 1 ----------- 2 | 5 | 1 ----------- 2 | 2 | 1 ----------- 2 | 1 | 0 decimal value (base 10) 47 = binary bits (base 2) 101111.
Binary to Decimal conversion - algorithm
Given binary bits, converts it into a decimal value
- set decval==0
- Read a LSB bit from binary-bits
- decval + LSB x 2 power of the LSB position value
- Next LSB
- print decval
binary to decimal value conversion - Example
Given binary bits = 10111 ,convert it into a decimal value. A bit position value(0 to n-1 bits) starts right to left on the binary bits. = 1 x 24 + 0 x 23 + 1 x 22 + 1x 21 + 1 x 20 = 1x16 + 0x8 + 1x4 + 1x2 + 1x1 = 16 + 0 + 4 + 2 + 1 decimal value = 23 binary bits (base 2) 10111 = 23 decimal value (base 10).
Number System Conversion - Java Programming Code
The Java program converts a decimal number to binary numbers and binary bit sequence to a decimal value.
import java.util.Scanner;
public class Conversion {
public static String dec2bin(int decval) {
StringBuffer bits=new StringBuffer();
while ( decval>1 )
{
bits.append( decval % 2 );
decval = decval/2;
}
if ( decval ==1)
bits.append( decval );
return bits.reverse().toString();
}
public static int bin2dec(String bits) {
int decval =0;
StringBuffer sbits =new StringBuffer(bits);
sbits.reverse();
for(int n=0;n<sbits.length();n++)
{
if ( sbits.charAt(n)=='1')
decval = decval +(int)Math.pow(2, n);
}
return decval;
}
public static void main(String[] args) {
int decval=0; String bstr="";
System.out.println("nt Number System Conversion ");
System.out.println("Decimal to Binary / Binary to Decimal");
System.out.println("\n Select Choice \t 1 - Binary2Decimal \t
2- Decimal2Binary \n ");
Scanner sc=new Scanner(System.in);
int choice =sc.nextInt();
switch ( choice ) {
case 1:
System.out.println("Enter Binary bits");
bstr= sc.next();
decval = Conversion.bin2dec(bstr);
System.out.println("nDecimal value :" + decval);
break;
case 2:
System.out.println("Enter positive Decimal value ");
decval = sc.nextInt();
bstr = Conversion.dec2bin(decval);
System.out.println("nBinary bits :" + bstr);
break;
}
sc.close();
}
}
Number System Conversion - Java program output
Number System Conversion Decimal to Binary / Binary to Decimal Select Choice 1 - Binary2Decimal 2- Decimal2Binary 2 Enter positive Decimal value 527 Binary bits :1000001111 Number System Conversion Decimal to Binary / Binary to Decimal Select Choice 1 - Binary2Decimal 2- Decimal2Binary 1 Enter Binary bits 110000111 Decimal value :391
Comments
Post a Comment