Concepts of Conversion of Base 10 Number to Base 2 Fixed Point Register Binary Number Representation

Sean Rodby, Luke Snyder, Autar Kaw
University of South Florida
United States of America
kaw@eng.usf.edu
Website: http://numericalmethods.eng.usf.edu
Version: Mathematica 6; Last Revised: August 28, 2008

Initialization

Clearing the definitions of all symbols in the current context:

ClearAll[Evaluate[Context[]<>"*"]]

Introduction

The following worksheet illustrates how to convert a positive decimal number to a fixed point register binary number using loops and various conditional statements. The user inputs a decimal number in the Input section of the program, and then an equivalent binary number is given as an output.

Section 1: Input Data

This is the only section where the user interacts with the program.

Enter positive real number to be converted to binary number

decnum=54.3;

Enter number of bits to be used in integer part

m=8;

Enter number of bits to be used in fractional part

n=6;

This is the end of the user section. All information must be entered before proceeding to the next
section. RE-EVALUATE THE NOTEBOOK.

Section 2: Procedure

Calculating the minimum and maximum possible base-10 values based on the number of bits specified by the user.

sum1=0;
Do[sum1=sum1+(2^i),{i,0,m-1}]
sum2=0;
Do[sum2=sum2+(2^(-i)),{i,n}]

maxval=N[sum1+sum2,10]

minval=N[(2^(-n)),10]

If the maximum value that can be produced given the user specified number of bits is smaller than the number to be represented, then more bits are needed. Similarly, if the minimum value that can be produced given the user specified number of bits is larger than the number to be represented, a change in bits is needed.

If[maxval<decnum,Print["Since |",decnum,"| > ",maxval ," the number of bits specified is not sufficient to represent this number in floating point representation. Either specify fewer mantissa bits, or more total bits for the worksheet."]]
If[minval>decnum,Print["Since ",minval," > |",decnum,"|, the number of bits specified is not sufficient to represent this number in floating point representation. Either specify fewer mantissa bits, or more total bits for the worksheet."]]
If[minval≤decnum∧maxval≥decnum,Print["Since ",minval," < |",decnum,"| < ",maxval," the base-10 number can be represented in floating point binary format."]]

Using the Floor command to isolate each section of the decimal number to begin binary number conversion

beforedecimal=Floor[decnum]

afterdecimal=decnum-Floor[decnum]

Using a loop to perform the binary conversion repetitively until the necessary number of bits are used. Using the floor command to calculate the quotient of the decimal number. Using a combination of the Ceiling and Floor commands to determine the remainder, and inputting this value into an array. Then calculating the binary conversion of the value before the decimal. Here we use the variable bold to denote the value of the integer part of the decimal number.

bold=beforedecimal;
j=1;
Do[
quotient=Floor[(bold/2)];
(Subscript[Binary, i])=Ceiling[(bold/2)]-Floor[(bold/2)];
If[quotient==0 ∧beforedecimal≠0,
(Subscript[Binary, i])=1;
Break[]];
If[beforedecimal==0,
(Subscript[Binary, i])=0;
Break[]];
bold=quotient;
j=j+1,
{i,m}]

Using this method, the placement of the binary numbers in the array Binary are backwards and so must be flipped around to form the actual binary number.

Do[(Subscript[BinaryBeforeDec, i])=(Subscript[Binary, j+1-i]),{i,j}]

Converting the fractional part of the decimal into a binary number. Here we initialize the sum using a new value, boldA, which represents the value after the decimal point.

boldA=afterdecimal;
Do[
Num=boldA*2;
If[Num<1,
(Subscript[BinaryAfterDec, i])=0];
If[Num≥1,
Num=Num-Floor[Num];
(Subscript[BinaryAfterDec, i])=1];
boldA=Num,
{i,n}]

Combining the binary numbers and determining the number of terms in the resulting base 2 number. Then displaying the results.

str1="";
Do[str1=StringJoin[str1, ToString[(Subscript[BinaryBeforeDec, i])]],{i,j}];
str2="";
Do[str2=StringJoin[str2, ToString[(Subscript[BinaryAfterDec, i])]],{i,n}];
FinalBinNum=StringJoin[str1,".",str2]

Using a loop to sum values of the integer portion of the base-2 number. The loop variable sumint is used for summation and is initialized at 0.

intbin=Floor[ToExpression[FinalBinNum]];
strint=ToString[intbin];
m=StringLength[strint];
n=StringLength[FinalBinNum];

sumint=0;
Do[
(Subscript[binint, i])=ToExpression[StringTake[strint,{i}]];
sumint=sumint+(Subscript[binint, i])*(2^(m-i)),
{i,m}]

Using a loop to sum values of the fractional portion of the base-2 number. The loop variable sumfrac is used for summation and is initialized at 0.

sumfrac=0;
j=1;
Do[
(Subscript[binfrac, j])=ToExpression[StringTake[FinalBinNum,{i}]];
sumfrac=sumfrac+(Subscript[binfrac, j])*(2^(-j));
j=j+1,
{i,m+2,n}]

Adding the fractional portion of the base-2 number with the integer portion which yields the base-10 number.

totaldec=N[sumint+sumfrac,10]

Finding the total relative percentage difference between the representation and the number to be represented.

totpercdiff=((decnum-totaldec)/decnum)*100

Conclusion

This worksheet illustrates the use of Mathematica to convert a base-10 number to a base-2 binary number. It is important to understand the binary system as it has numerous applications. Critical to this understanding is being able to convert binary numbers to base-10 numbers, and vice-versa.

References

Binary Representation of Numbers
See: http://numericalmethods.eng.usf.edu/nbm/gen/01aae/nbm_gen_aae_txt_binaryrepresentation.pdf

Legal Notice: The copyright for this application is owned by the author(s). Neither Wolfram Research nor the author(s) are responsible for any errors contained within and are not liable for any damages resulting from the use of this material. This application is intended for non-commercial, non-profit use only. Contact the author for permission if you wish to use this application in for-profit activities.