Solved CSE1320-004/005 HW 3 Purpose: Use bit operations to extract information

$30.00

Category: Tags: , , , , , You will Instantly receive a download link for .zip solution file upon Payment || To Order Original Work Click Custom Order?

Description

5/5 - (1 vote)

Purpose:

  • Use bit operations to extract information
  • Demonstrate understanding of number formats and binary representations

 

Assignment:

 

Computers sometimes use a format called binary coded decimal or ‘BCD’.  Good background information is available from https://en.wikipedia.org/wiki/Binary-coded_decimal , but it is far deeper than you need for this assignment. (read at your own risk, the section marked “Basics” covers all you need to know at this point in time).

 

BCD encodes a decimal number into 4 binary digits.  A byte holds 2 BCD digits.   A 32 bit word can contain 8 packed BCD digits.

 

Decimal digit BCD
8 4 2 1
0 0 0 0 0
1 0 0 0 1
2 0 0 1 0
3 0 0 1 1
4 0 1 0 0
5 0 1 0 1
6 0 1 1 0
7 0 1 1 1
8 1 0 0 0
9 1 0 0 1

 

Example:

 

The decimal number 4660.

 

Expressed as hex, it is 0x1234.

 

In BCD, it is 0100 0110 0110 0000 or 4660 BCD or 18016 base 10.

 

The decimal number 9999.  As hex it is 0x270F

 

In BCD, it is 1001 1001 1001 1001, or 9999 BCD, or 39,321 base 10.

 

Write a program that accepts an integer input (base 10) to stdin.   This input value is in packed BCD format.  Using the packed BCD format, display the number as a sequence of characters to stdout.  The sequence of characters is a simulation of a calculator’s display (5 characters wide, 5 characters tall, with a space between each).

 

  1. Display any BCD digit as a 0 if it is an invalid encoding.
  2. All numbers are positive.
  3. Note that the number provided as input data is in packed BCD representation.
  4. Process input until a -1 is entered. Do not process the -1, just finish without printing anything out.
  5. Output must match the example exactly. Don’t try and make the ‘calculator display’ more readable.
  6. Use bit operations to extract the digits.
  7. The program must work for any valid input values; provided they do not overflow an integer. Suggest you test with 10 or 20 different values prior to turning it in.
  8. A two dimensional array is useful, but not essential for completing this program.
  9. Including comments, this assignment can be completed in less than 100 lines of code. If you are writing much more than this, you are on the wrong track.  Ask for help.
  10. Every function must have comments that describe its purpose,implementation, and interface.
  11. Any pre and post conditions for a function must be described in the comments, and checked at runtime using assert ().
  12. As in previous assignments, suggest you use bash input redirection to make a test file to verify you program works with many inputs.
  13. The program will be contained in a single C file. The file shall be named hw03_last_name_first_name.c .
  14. Compile with -std=c99
  15. Turn into blackboard a single C file.

 

 

Grading Criteria:

 

  1. Correct output, observed by running the code against several input files; each input file containing several input values.
  2. Indentation, commenting of code
  3. Compliance with the assignment
  4. Compiles without warnings using -Wall

 

 

Example Output:

 

$ gcc -Wall -std=c99 -g hw4.c

$ ./a.out

4660

—   —   —   —        —-  —-

|   | |   | |   | |   |     |    /      | |

|   | |   | |   | |   |     |   /   —-  —-

|   | |   | |   | |   |     |  /        |   ^

—   —   —   —        —-  —-    ^

305419896

—-  —-       —-        —-   —

|    /      | |    |     |        /  |   |

|   /   —-  —- —-   —    /    —

|  /        |   ^      | |   |  /    |   |

—-  —-    ^  —-   —         —

39321

—   —   —   —   —   —   —   —

|   | |   | |   | |   | |   | |   | |   | |   |

|   | |   | |   | |   |  —   —   —   —

|   | |   | |   | |   |     |     |     |     |

—   —   —   —

-1