This preview shows pages 1–2. Sign up to view the full content.
CS240 Spring 2011: PROJECT1
Goal
Implement routines for encoding, decoding, comparing, adding, subtracting, multiplying and dividing
BCD (Binary Coded Decimal) numbers.
BCD
To encode a decimal number using the common BCD encoding, each decimal digit is stored in a 4bit
nibble. Thus, the BCD encoding for the number 127 would be: 0001 0010 0111. Whereas the pure binary
number would be: 0111 1111.
Unlike binaryencoded numbers, BCDencoded numbers can easily be displayed by mapping each of the
nibbles to a different character. Converting a binaryencoded number to decimal for display is much
harder, as this generally involves integer multiplication or divide operations. BCD also avoids problems
where fractions that can be represented exactly in decimal cannot be represented in binary (e.g., one
tenth).
To illustrate this problem, run the following program and observe its output.
#include <stdio.h>
int main()
{
float g;
int i;
g = 0.0;
for (i = 0; i < 10000; i++)
g += 0.1;
printf("%g\n", g);
}
Since computers store data in 8bit bytes, there are two common ways of storing 4bit BCD digits in
bytes:
1. each digit is stored in one nibble of a byte, with the other nibble being set to all zeros, all ones (as
in the EBCDIC code), or to 0011 (as in the ASCII code)
2. two digits are stored in each byte.
For this project, we will only work with nonnegative integers. We will use character arrays to store BCD
numbers. The first element (or 0) of the array will be used to track how many digits are in the number.
The other elements 1 through N will store one BCD digit each. Specifically, element i will store the digit
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview. Sign up
to
access the rest of the document.
 Spring '08
 REGO

Click to edit the document details