Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Binary to binary coded decimal conversion problem (http://www.go4expert.com/forums/binary-binary-coded-decimal-conversion-t1038/)

LaVic 21Jul2006 07:22

Binary to binary coded decimal conversion problem
 
Hello Everyone,

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,00,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,00,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,01,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);
}


shabbir 21Jul2006 10:43

Re: Binary to binary coded decimal conversion problem
 
Welcome to G4EF and please use proper bbcode for the code snippets you put in.

Also can you elaborate whats the problem you are facing with the code.

LaVic 21Jul2006 20:03

Re: Binary to binary coded decimal conversion problem
 
Sorry !

The problem that i am having is that i am not getting what i expect to get if i follow the example of converting 1111 1111 to a binary coded decimal (see link).



I test my program by changing the NUM variable in the following line close at the end to the program.

while ( num < 5); //designates how many times the processed array is shifted

"num" basically tells the program how many times the processed array is being shitfted during the conversion process.

Usually the should 8 shifts for an 8 bit array , 4 shift for a 4 bit array and so on.

This the actual array that is being processed: A[20] = {1,1,1,1, 1,1,1,1, 0,0,0,0, 0,0,0,0, 0,0,0,0};
It is being read from left to right.

The output that i am getting if i enter num<4 is and have array A[20] as 1111 1111 is:

//////////////////////////////

original 8 bit binary number 1 1 1 1 1 1 1 1
the UNITS bits are 0 0 0 0
the TENS bits are 0 0 0 0
the HUNDREDS bits are 0 0 0 0

I am in the loop <== used for debugging

shifted array A[1] = 0
shifted array A[2] = 1
shifted array A[3] = 1
shifted array A[4] = 1
shifted array A[5] = 1
shifted array A[6] = 1
shifted array A[7] = 1
shifted array A[8] = 1
shifted array A[9] = 1
shifted array A[10] = 0
shifted array A[11] = 0
shifted array A[12] = 0
shifted array A[13] = 0
shifted array A[14] = 0
shifted array A[15] = 0
shifted array A[16] = 0
shifted array A[17] = 0
shifted array A[18] = 0
shifted array A[19] = 0
shifted array A[20] = 0

UNITsum = 1

TENSsum = 0

HUNDSsum = 0

the units BCD is equal to 1 in decimal
the tens BCD is equal to 0 in decimal
the hundreds BCD is equal to 0 in decimal I am in the loop

shifted array A[1] = 0
shifted array A[2] = 0
shifted array A[3] = 1
shifted array A[4] = 1
shifted array A[5] = 1
shifted array A[6] = 1
shifted array A[7] = 1
shifted array A[8] = 1
shifted array A[9] = 1
shifted array A[10] = 1
shifted array A[11] = 0
shifted array A[12] = 0
shifted array A[13] = 0
shifted array A[14] = 0
shifted array A[15] = 0
shifted array A[16] = 0
shifted array A[17] = 0
shifted array A[18] = 0
shifted array A[19] = 0
shifted array A[20] = 0

UNITsum = 1

TENSsum = 0

HUNDSsum = 0

the units BCD is equal to 3 in decimal
the tens BCD is equal to 0 in decimal
the hundreds BCD is equal to 0 in decimal I am in the loop


shifted array A[1] = 0
shifted array A[2] = 0
shifted array A[3] = 0
shifted array A[4] = 1
shifted array A[5] = 1
shifted array A[6] = 1
shifted array A[7] = 1
shifted array A[8] = 1
shifted array A[9] = 1
shifted array A[10] = 1
shifted array A[11] = 1
shifted array A[12] = 0
shifted array A[13] = 0
shifted array A[14] = 0
shifted array A[15] = 0
shifted array A[16] = 0
shifted array A[17] = 0
shifted array A[18] = 0
shifted array A[19] = 0
shifted array A[20] = 0

UNITsum = 7

TENSsum = 0

HUNDSsum = 0

the units BCD is equal to 7 in decimal
the tens BCD is equal to 0 in decimal
the hundreds BCD is equal to 0 in decimal I am in the loop

shifted array A[1] = 0
shifted array A[2] = 0
shifted array A[3] = 0
shifted array A[4] = 0
shifted array A[5] = 1
shifted array A[6] = 1
shifted array A[7] = 1
shifted array A[8] = 1
shifted array A[9] = 1
shifted array A[10] = 1
shifted array A[11] = 0
shifted array A[12] = 0
shifted array A[13] = 1
shifted array A[14] = 0
shifted array A[15] = 0
shifted array A[16] = 0
shifted array A[17] = 0
shifted array A[18] = 0
shifted array A[19] = 0
shifted array A[20] = 0

UNITsum = 5

TENSsum = 1

HUNDSsum = 0

the units BCD is equal to 5 in decimal
the tens BCD is equal to 1 in decimal
the hundreds BCD is equal to 0 in decimal I am in the loop

shifted array A[1] = 0
shifted array A[2] = 0
shifted array A[3] = 0
shifted array A[4] = 0
shifted array A[5] = 0
shifted array A[6] = 1
shifted array A[7] = 1
shifted array A[8] = 1
shifted array A[9] = 1
shifted array A[10] = 0
shifted array A[11] = 0
shifted array A[12] = 0
shifted array A[13] = 1
shifted array A[14] = 1
shifted array A[15] = 0
shifted array A[16] = 0
shifted array A[17] = 0
shifted array A[18] = 0
shifted array A[19] = 0
shifted array A[20] = 0

UNITsum = 1

TENSsum = 3

HUNDSsum = 0

the units BCD is equal to 1 in decimal
the tens BCD is equal to 3 in decimal
the hundreds BCD is equal to 0 in decimal I am in the loop

array outside loop A[1] = 0
array outside loop A[2] = 0
array outside loop A[3] = 0
array outside loop A[4] = 0
array outside loop A[5] = 0
array outside loop A[6] = 1
array outside loop A[7] = 1
array outside loop A[8] = 1
array outside loop A[9] = 1
array outside loop A[10] = 0
array outside loop A[11] = 0
array outside loop A[12] = 0
array outside loop A[13] = 1
array outside loop A[14] = 1
array outside loop A[15] = 0
array outside loop A[16] = 0
array outside loop A[17] = 0
array outside loop A[18] = 0
array outside loop A[19] = 0
array outside loop A[20] = 0

Units = 1

Tens = 3

Hunds = 0

number of shifts = 5

///////////////////////////////////////////

shabbir 21Jul2006 21:17

Re: Binary to binary coded decimal conversion problem
 
You are missing couple of point from the link algo.

1. There should be 8 shifts for the 8 bit number.
2. It should be right to left shifting otherwise your calculation for greater than 5 for adding 3 can be wrong.


All times are GMT +5.5. The time now is 13:45.