![]() |
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
|
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. |
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 /////////////////////////////////////////// |
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 10:59. |