0% found this document useful (0 votes)
126 views21 pages

Binary To Decimal

The document discusses different methods to convert between binary, decimal, and hexadecimal number systems. It begins by explaining how to convert a binary number to decimal by summing the place values. It then provides C++ code examples to demonstrate: 1) A basic but inefficient conversion method using pow() 2) A more efficient method pre-calculating the place values 3) Horner's method to minimize multiplication It further explores converting hexadecimal to decimal and representing decimal fractions in binary. Recursive functions are demonstrated to convert decimal to binary fractions.

Uploaded by

Le Putra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views21 pages

Binary To Decimal

The document discusses different methods to convert between binary, decimal, and hexadecimal number systems. It begins by explaining how to convert a binary number to decimal by summing the place values. It then provides C++ code examples to demonstrate: 1) A basic but inefficient conversion method using pow() 2) A more efficient method pre-calculating the place values 3) Horner's method to minimize multiplication It further explores converting hexadecimal to decimal and representing decimal fractions in binary. Recursive functions are demonstrated to convert decimal to binary fractions.

Uploaded by

Le Putra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 21

Binary to decimal

In base 2 all digits are either 0 or 1 and can be interpreted as


10111 = 1 x 1 + 1 x 2 + 1x 4 + 0 x 8 + 1 x 16
=1 x 20 +1 x 21 + 1 x 22 +0 x 23 +1 x 24 = 23( base 10)
If b is an array containing these 5 digits so that b[0]=1, b[1]=1,
b[2]=1,b[3]=0, b[4]=1, how do we write code to get to the decimal
equivalent?

Inefficient Binary to decimal code


#include <iostream.h>
#include <math.h>
int main()
{
/* to convert a binary representation to a decimal one*/
int dec=0,b[5]={1,1,1,0,1};
for (int i=0;i<5;i++)
dec+=b[i]*pow(2,i); // really
inefficient cout << dec << endl;
return 0;
2

Somewhat efficient code-10 multiplications

int main()
{

/* to convert a binary representation to a decimal one*/


int dec=0, b[5]={1,1,1,0,1}, power2=1;
for (int i=0;i<5;i++)
{
dec+=b[i]*power2;
power2*=2;
}
cout << dec << endl;

return 0;
}

Horners scheme- 4 multiplications


1 x 20 +1 x 21 + 1 x 22 +0 x 23 +1 x 24 =1 + 2 x(1 + 2x(1 + 2x(0 + 2x1)))
Recall b[0]=1, b[1]=1, b[2]=1,b[3]=0, b[4]=1

/* to convert a binary representation to a decimal one*/


int dec,b[5]={1,1,1,0,1};
dec =b[4];
for (int i=3;i>=0;i--)
{
dec=2*dec+b[i]; //horner's scheme
}
6

cout << dec << endl;

Horners scheme via recursion

/* to convert a binary representation to a decimal


one*/ int dec,b[5]={1,1,1,0,1};
cout <<horner(b,0,4)<<
endl;

int horner(int b[ ],int i,int n);


int main()
{
return 0;
}
8

int horner(int b[ ],int i,int n)


{

if (i==n)
return b[n];

//base
case

else
return 2*horner(b,i+1,n)+b[i];
}

Hex to decimal

int horner(char hex[ ],int i,int


n); int main()
{
/* to convert a hexadecimal representation to a decimal
one*/ char hex[]="C1A"; //this is A*256 + 16 +A
cout
<<horner(hex,0,2);
return 0;
}
int horner(char hex[ ],int
i,int n) {int d;
if (hex[i]>='A')
d=10+hex[i]-'
A';
else
10

d=hex[i]-'0';
if (i==n)
return d;
else
return 16*horner(hex,i+1,n)+d;
}

11

/* to convert a hexadecimal representation to a decimal one*/

Hex to decimal reverse


ordering
int horner(char hex[ ],int n);
int main()
{
char hex[]="A1C"; //this is A*256+1*16 +C
cout <<horner(hex,2);
return 0;
}
int horner(char hex[ ],int
n) {int d;
if (hex[n]>='A')
d=10+hex[n]-'
A';
12

else
d=hex[n]-'0';
if (n==0)
return d;
else
return 16*horner(hex,n-1)+d;
}

13

Decimal to binary fractions


.625 = 6 x 10-1 + 2 x 10-2 + 5x 10-3
If .111 were a fraction in binary, then
.111 =1 x 2-1 + 1 x 2-2 +1 x 2-3 = .5 +.25+.125 =.875
Let us represent .625 as
a 2-1 + b 2-2 +c 2-3 =.abc in binary
And try to find a,b, and c.
note that 2 x .625 = a 20 + b 2-1 +c 2-2 = 1.250
so integer part gives us a=1
fractional part is b 2-1 +c 2-2 =.250
multiply fractional part by 2 and integer part = 0= b
fractional part of .5 is c 2-1 so multiplying by 2 and taking
14

integer part gives c=1

15

Binary equivalent of .1

2 x .1 =.2

integer part =0

2 x .2 =.4

integer part =0

2 x .4 =.8

integer part =0

2 x .8 =1.6

integer part =1

2 x .6=1.2

integer part =1

2 x .2 =.4

integer part =0

Solution is .000110 0110 0110 0110 0110


It never ends so that if one cuts it at some point, there is always an
16

error-Look at http://www.ima.umn.edu/~arnold/disasters/patriot.html
To find out why this was once very important.

17

C++ for converting decimal to binary fractions

double frac=.1;
int digits=1,intpart;
cout <<".";
while (digits <32 && frac!=0.)
{
frac=frac*2;
intpart=frac;
frac=frac-intpart;
cout <<intpart;
digits++;
}
18

Output: .0001100110011001100110011001100

19

Decimal to binary fraction recursive function


void fractobin(double frac, int
digits); int main()
{
cout <<".";
fractobin(.1,1); //using a recursive function
return
0;
}
void fractobin(double frac, int digits)
{
int intpart;
if (digits >=32 || frac==0.)
return;
//base case
else
{
frac=frac*2;
//get integer
intpart=frac;
part
cout <<intpart;

}21

fractobin(frac-intpart,++digits); //call self and increment


digits return;

You might also like