Problem Set 5Representing Numbers |
Out: 22 March Due: Wednesday (beginning of class), 29 March |

**Collaboration Policy - Read Carefully** (similar to PS4)

For this assignment, you may work on your own or with any one other
person of your choice **except for anyone you worked with on PS3 or PS4**.
If you work with a partner, you should turn in one assignment with both
of your names on it. **If you would prefer to
be assigned a partner, send email to evans@cs.virginia.edu
before 5pm on Friday, 24 March** (include any constraints or
preferences you have on your assigned partner). If a suitable match
requests a partner, you will receive a partner assignment. Partners
will be assigned using a greedy algorithm based on when requests arrive,
so you are more likely to receive a suitable partner assignment if you send in
your request early.

You may consult any outside resources including books, papers, web sites and people you wish, except you may not copy code from other big number representation implementations. There are many implementations of big numbers in C available on the web (for example, there is one in the Python interpreter and GMP), and it would certainly defeat the purpose of this assignment if you copied one of them instead of thinking on your own. You may look at design documents for big integer implementations is you wish, but we recommend thinking about your own design first, and only looking for other designs if you are really stuck.

You are also encouraged to discuss these problems with students in the class. You must acknowledge any people and outside resources you work with on your assignment. If you discuss the assignment with people other than your partner, you may not take any written materials out of your discussion. It is fine to bounce ideas off other people, but the answers you turn in must be your own.

You are **strongly encouraged** to take advantage of the staffed lab
hours posted on the CS216 web site.

**Purpose**

- Learn how numbers are represented.
- Discover experimentally properties of your C implementation
- Implement a variable-length integer encoding in C that is better than Microsoft's (Excel)

Here, we will consider a simple hypothetical smart card with one 8-bit
register using big-endian encoding. The current used for each
operation scales linearly with the number of bits in the register that
flip (either a 0 becomes a 1, or a 1 becomes a 0). The card implements
a simple counter: the value in the register increases by one each clock
tick. So, if the register initially contains `0000 0011` after
the next clock ticks the resuting value in the register will be `0000
0100` and 3 units of current will have been consumed.

Your goal is to determine the number stored in the register, just by observing the current consumed by the card.

4 | ||||||

3 | ||||||

2 | ||||||

1 | ||||||

1 | 2 | 3 | 4 | 5 | 6 | |

Clock Tick |

**2.** What is the fewest number of additional clock tick current
readings that could be enough to uniquely determine the exact value stored on the
card?

(Note that the product of any two numbers ending in 9 should end in a 1, so Excel must be wrong. The number on the bottom line is the current US national debt to give a sense that numbers of this scale could meaningfully occur in spreadsheets.)

For the next questions your goal is to design and implement a variable length integer implementation that is more accurate than Excel's.

The desired properties are:

- Use one word (or less) for small numbers
- Be able to store arbitrarily large numbers
- Be able to perform addition and multiplication efficiently

bignum bignum_create (char *p_x); /* post: returns a bignum object representing the value p_x where p_x is a decimal number represented as a string */ char *bignum_unparse (bignum p_b); /* post: returns a string representation of p_b that shows its decimal value */

bignum bignum_add (bignum p_b1, bignum p_b2); /* post: returns a bignum representing the value p_b1 + p_b2 */

bignum bignum_mult (bignum p_b1, bignum p_b2); /* post: returns a bignum representing the value p_b1 * p_b2 */

printf ("%s\n", bignum_unparse (bignum_mult (bignum_create ("99999999"), bignum_create ("99999999"))));to demonstrate that your number representation is more accurate than Excel's.

Remember to submit your `bignum.c` and `bignum.h` files here:
*http://www.cs.virginia.edu/cs216/ps/ps5/submit.html*.

The
submission program will run your `bignum` implementation on
several test cases and check it produces the correct answers.
Submitting your code electronically is not a substitute for turning in
your answers (including all your bignum implementation code) on paper.

CS216: Program and Data RepresentationUniversity of Virginia |
David Evansevans@cs.virginia.eduUsing these Materials |