530 likes | 1.8k Views
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 2 0 +1 x 2 1 + 1 x 2 2 +0 x 2 3 +1 x 2 4 = 23( base 10)
E N D
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; }
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; }
Horner’s 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 } cout << dec << endl;
Horner’s scheme via recursion int horner(int b[ ],int i,int n); int main() { /* to convert a binary representation to a decimal one*/ int dec,b[5]={1,1,1,0,1}; cout <<horner(b,0,4)<< endl; return 0; } 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 d=hex[i]-'0'; if (i==n) return d; else return 16*horner(hex,i+1,n)+d; }
int horner(char hex[ ],int n); int main() { /* to convert a hexadecimal representation to a decimal one*/ 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'; else d=hex[n]-'0'; if (n==0) return d; else return 16*horner(hex,n-1)+d; } Hex to decimal reverse ordering
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 integer part gives c=1
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 error- Look at http://www.ima.umn.edu/~arnold/disasters/patriot.html To find out why this was once very important.
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++; } Output: .0001100110011001100110011001100
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; intpart=frac; //get integer part cout <<intpart; fractobin(frac-intpart,++digits); //call self and increment digits return; } }