prj1 - CS240 Spring 2011 PROJECT1 Goal Implement routines...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
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 4-bit nibble. Thus, the BCD encoding for the number 127 would be: 0001 0010 0111. Whereas the pure binary number would be: 0111 1111. Unlike binary-encoded numbers, BCD-encoded numbers can easily be displayed by mapping each of the nibbles to a different character. Converting a binary-encoded 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 8-bit bytes, there are two common ways of storing 4-bit 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 non-negative 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
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/23/2012 for the course CS 240 taught by Professor Rego during the Spring '08 term at Purdue.

Page1 / 3

prj1 - CS240 Spring 2011 PROJECT1 Goal Implement routines...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online