I am new here. I would appreciate any help on a program that i am currently putting together. My program is supposed to convert 8 bit binary numbers to binary coded decimal (BCD) numbers. e.g.:
The binary number 1111 1111 (255 in decimal) should equal 0010 0101 0101 in BCD, where 0010 => 2 (for the hundreds position), and 0101 => 5 (for the tens and units position)
Conversion is primarily achieved by shifting and addding 3 (0011 in binary) several times.
The binary to binary coded decimal conversion follows the follwing rules:
#1 Shift the binary number left one bit.
#2 If 8 shifts have taken place, the BCD number is in the Hundreds, Tens, and Units column.
#3 If the binary value in any of the BCD columns is 5 or greater, add 3 to that value in that BCD column.
#4 Go to 1.
The program outputs the answer in form units, tens and hundreds in decimal form
correct answer should be: (after 8 shifts)
e.g.: Units = 2
Tens = 5
Hunds = 5
LINK: http://www.engr.udayton.edu/faculty/...in_to_BCD.html
I would appreciate any help. Thank you all.
My program:
Code: CPP
// 8 BIT BINARY TO BINARY CODED DECIMAL /////////////////////
#include <stdio.h>
#include <cmath>
#include <iostream>
using std::cout;
using std::endl;
void shifting(int *arr,int max)
{
int temp;
//int sh;
int i,n;
for(i=0;i<1;i++) // designates how many spaces u want the binary number shifted e.g.: i<1 => 1 unit shift to the right
{
temp=*(arr+max-1);
for(n=max-2;n>=0;n--)
*(arr+n+1)=*(arr+n);
*arr=temp;
}
}
int main()
//binary number are READ FROM LEFT TO RIGHT !!! least sig. dig. to most sig. dig. !!!
{ // |8-bit binary # | Units | Tens |Hundreds|
int A[20] = {1,1,1,1, 1,1,1,1, 0,0,0,0, 0,0,0,0, 0,0,0,0};
// |8-bit binary # | Units | Tens |Hundreds|
int UNITSaddition[20] = {0,0,0,0, 0,0,0,0, 1,1,0,0, 0,0,0,0, 0,0,0,0 }; //for adding 3 to the units BCD
int TENSaddition[20] = {0,0,0,0, 0,0,0,0, 0,0,0,0, 1,1,0,0, 0,0,0,0 }; //for adding 3 to the units BCD
int HUNDSaddition[20] = {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 1,1,0,0 }; //for adding 3 to the units BCD
int total = 0; int UNITSsum = 0; int TENSsum = 0; int HUNDSsum = 0;
int doubleval[21]; int HUNDSval[19]; int TENSval[19]; int UNITSval[19];
/*for (int i = 0; i < 21 ; i++)
{
doubleval[i] = (A[i]*pow(2,i));
printf("\n position value %i in decimal => %d ", i, doubleval[i]);
total += doubleval[i];
} */
//// DATA OVERVIEW module ////////////////////////////////////////////
// printf("\n the binary number is equal to %d in decimal ", total);
printf("\n original 8 bit binary number %i %i %i %i %i %i %i %i", A[0],A[1],A[2],A[3],A[4],A[5],A[6],A[7] );
printf("\n the UNITS bits are %i %i %i %i ", A[8],A[9],A[10],A[11]);
printf("\n the TENS bits are %i %i %i %i ", A[12],A[13],A[14],A[15]);
printf("\n the HUNDREDS bits are %i %i %i %i \n", A[16],A[17],A[18],A[19]);
////////////////////////////////////////////////////////////////////////////
// int arr[100], N[20];
int max = 20; // max = Max size of the array
int num = 0;
do
{
UNITSsum = 0;
TENSsum = 0;
HUNDSsum = 0;
shifting(A,max); // actual shifting function call
printf("I am in the loop \n");
for(int i=0;i<max;i++)
printf("\n shifted array A[%d]= %d ",i+1, *((A + i)));
printf("\n");
num++;
// N[i] = A[i];
// for( i=0;i<max;i++)
//printf("\n shifted array N[%d]= %d ",i+1, N[20]);
// CALCULATION module 1 ////////////////////////////////////////////////////
UNITSval[8] = (A[8]*pow(2,0));
UNITSval[9] = (A[9]*pow(2,1));
UNITSval[10] = (A[10]*pow(2,2));
UNITSval[11] = (A[11]*pow(2,3));
for (i = 8; i <12; i++)
{
UNITSsum += UNITSval[i]; //the 4-bit units position in decimal
}
printf("\nUNITSsum = %d\n", UNITSsum);
TENSval[12] = (A[12]*pow(2,0));
TENSval[13] = (A[13]*pow(2,1));
TENSval[14] = (A[14]*pow(2,2));
TENSval[15] = (A[15]*pow(2,3));
for (i = 12; i <16; i++) //the 4-bit tens position in decimal
{
TENSsum += TENSval[i];
}
printf("\nTENSSsum = %d\n", TENSsum);
HUNDSval[16] = (A[16]*pow(2,0));
HUNDSval[17] = (A[17]*pow(2,1));
HUNDSval[18] = (A[18]*pow(2,2));
HUNDSval[19] = (A[19]*pow(2,3));
for (i = 16; i <20; i++) //the 4-bit hundreds position in decimal
{
HUNDSsum += HUNDSval[i];
}
printf("\nHUNDSsum = %d\n", HUNDSsum);
//// if the binary value of any of the BCD columns is 5 or greater, add 3 to that value in that BCD column.
int overflow[21];
int array_value3[20];
if (HUNDSsum >= 5) // add 3 to orignal binary number if hundreds sum >= 5
{
for (i=0; i<20; i++)
overflow[i] = 0;
for (i=0; i<20; i++)
{
array_value3[i] = A[i] + HUNDSaddition[i] + overflow[i];
if (array_value3[i] > 1)
{
overflow[i+1] = 1;
array_value3[i] %= 2;
}
for (i=0; i<20; i++)
{
A[i] = array_value3[i];
array_value3[i] = 0;
}
}
}
if (TENSsum >= 5) // add 3 to orignal binary number if tens sum >= 5
{
for (i=0; i<20; i++)
overflow[i] = 0;
for (i=0; i<20; i++)
{
array_value3[i] = A[i] + TENSaddition[i] + overflow[i];
if (array_value3[i] > 1)
{
overflow[i+1] = 1;
array_value3[i] %= 2;
}
for (i=0; i<20; i++)
{
A[i] = array_value3[i];
array_value3[i] = 0;
}
}
}
if (UNITSsum >= 5) // add 3 to orignal binary number if units sum >= 5
{
for (i=0; i<20; i++)
overflow[i] = 0;
for (i=0; i<20; i++)
{
array_value3[i] = A[i] + UNITSaddition[i] + overflow[i];
if ( array_value3[i] > 1)
{
overflow[i+1] = 1;
array_value3[i] %= 2;
}
}
for (i=0; i<20; i++)
{
A[i] = array_value3[i];
array_value3[i] = 0;
}
}
printf("\n the units BCD is equal to %d in decimal ", UNITSsum); // prints units 4-bit binary# in decimal
printf("\n the tens BCD is equal to %d in decimal ", TENSsum); // prints tens 4-bit binary# in decimal
printf("\n the hundreds BCD is equal to %d in decimal ", HUNDSsum); // prints hundreds 4-bit binary# in decimal
}
while ( num < 5); //designates how many times the orignal array is shifted (max should be 8 for 8 bits)
for(int i=0;i<max;i++)
printf("\narray outside loop A[%d]= %d ",i+1, A[i]);
printf("\n");
int temp_val1 = 0;
temp_val1 += (A[8]*pow(2,0));
temp_val1 += (A[9]*pow(2,1));
temp_val1 += (A[10]*pow(2,2));
temp_val1 += (A[11]*pow(2,3));
int temp_val2 = 0;
temp_val2 += (A[12]*pow(2,0));
temp_val2 += (A[13]*pow(2,1));
temp_val2 += (A[14]*pow(2,2));
temp_val2 += (A[15]*pow(2,3));
int temp_val3 = 0;
temp_val3 += (A[16]*pow(2,0));
temp_val3 += (A[17]*pow(2,1));
temp_val3 += (A[18]*pow(2,2));
temp_val3 += (A[19]*pow(2,3));
printf("\nUnits = %d\n", temp_val1);
printf("\nTens = %d\n", temp_val2);
printf("\nHunds = %d\n", temp_val3);
printf("\nnumber of shifts = %i ", num);
printf("\nBCD number = %i%i%i ",temp_val3, temp_val2,temp_val1);
//Prints the 16 bit binary number in decimal form
// for( i= 0; i <20; ++i) printf("\n d[%i] = %i ", i, array_value3[i]);
return (0);
}

