# Programming Assignment 2: Data Representation and Computer Arithmetic Solution

1 Introduction

This assignment is designed to help you learn the representation, interpretation, and manipulation of data as bits. There are two parts. In the first part, you will implement a program calc to add and subtract numbers specified in different bases (multiplication is extra credit). In the second part, you will implement a program format that will print the decimal values of bit sequences representing integer and floating point data types.

(Disclaimer : Make sure to follow university guidelines if looking at code.(IE. copying code goes against most rules.)

2 Numeric Base Conversion and Calculator

Implement a program called calc with the following usage interface:

calc [op] [number1] [number2] [output base]

The first argument, , is either the string “+”, for addition, or “-”, for subtraction. If you want to implement multiplication, then can also be the string “*”. (If you do implement multiplication, make sure to say so in your readme.pdf file so that the TAs know to check your program for this functionality.)

The next two arguments, and are integers of arbitrary size. Each of these numbers will be given in the form of:

−?(b|o|d|x)d n d n−1 ...d 1 d 0

which can be interpreted as: a number can optionally start with a ’−’ sign, followed by a base indicator, where b means the number is a binary number, o means octal, d means decimal, and h means hexadecimal. d n d n−1 ...d 1 d 0 are the digits of the number.

The final argument, output base, gives the base that the resulting number should be printed out in. Like the base indicator for the input numbers, this argument can be one of four strings: “b” for binary, “o” for octal, “d” for decimal, and “x” for hexadecimal. Your program should output the answer in the same form as the input numbers (that is, the output number should follow the regular expression given above).

Some examples:

$ ./calc + b1101 b1 d

d14

$ ./calc + d999999999 d1 d

d1000000000

$ ./calc - d10 -d4 b

b1110

$ ./calc + -d10 -d4 b

-b1110

Important: You must write the base conversion code yourself. You may not use type-casting, libraries, or output formats in printf(). You may use standard C arithmetic operations. You may use the C standard libraries for functionality not related to the conversion (e.g., string handling functions). Important: If calc detects an error in the inputs, it should print out an error message that starts with the string “ERROR”, followed by a string that gives an informative message about the error that it detected.

3 Format Interpretation

Implement a program called format with the following usage interface:

format [input bit sequence] [type]

The first argument, , is a sequence of 32 bits. Remember that your C program will get it as a string of 1 and 0 characters in the argv[1] argument to main. This sequence of bits represents the binary values stored in 4 contiguous bytes in memory. The leftmost bits are stored in the byte with the smallest address while the rightmost bits are stored in the byte with the largest address.

The second argument, , gives the type that you should use to interpret the input bit sequence, and can be either int (integer) or float.

The formats for the input bit sequence is as follows. If the type is:

int: the format is two’s complement;

float: the format is IEEE 754 single precision;

Note that the input bit sequence can correspond to negative numbers.

Your program should print out the decimal representation of the input bit sequence, assuming a big endian byte ordering. Floating point numbers should be printed in scientific notation, where a number 1.5x10 5 would be printed as 1.5e5. For positive infinity, output pinf, for negative infinity, output ninf, and for “NaN”, output NaN.

Here are some examples:

$ ./format 01000001010000100100001101000100 int

1094861636

$ ./format 10000001010000100100001101000100 int

-2126363836

$ ./format 01000001010000100100001110000100 int

1094861700

$ ./format 00000000000000000000000000000001 int

1

$ ./format 01000000110000110100001111010100 float

6.10203e0

$ ./format 00111010000111111111011000001000 float

6.1e-4

$ ./format 10000000000000000000000000000000 float

-0.0e0

$ ./format 01000000010010010000111111011011 float

3.141593e0

Important: You must write the interpretation code yourself. You may not use type-casting, libraries, or output formats in printf(). You may use standard C arithmetic operations. You may use the C standard libraries for functionality not related to the value interpretation (e.g., string handling functions).

Important: If format detects an error in the inputs, it should print out an error message that starts with the string “ERROR”, followed by a string that gives an informative message about the error that it detected. For this program, you can assume that any input bit sequence that is shorter or longer than 32 bits is erroneous.

This assignment is designed to help you learn the representation, interpretation, and manipulation of data as bits. There are two parts. In the first part, you will implement a program calc to add and subtract numbers specified in different bases (multiplication is extra credit). In the second part, you will implement a program format that will print the decimal values of bit sequences representing integer and floating point data types.

(Disclaimer : Make sure to follow university guidelines if looking at code.(IE. copying code goes against most rules.)

2 Numeric Base Conversion and Calculator

Implement a program called calc with the following usage interface:

calc [op] [number1] [number2] [output base]

The first argument, , is either the string “+”, for addition, or “-”, for subtraction. If you want to implement multiplication, then can also be the string “*”. (If you do implement multiplication, make sure to say so in your readme.pdf file so that the TAs know to check your program for this functionality.)

The next two arguments, and are integers of arbitrary size. Each of these numbers will be given in the form of:

−?(b|o|d|x)d n d n−1 ...d 1 d 0

which can be interpreted as: a number can optionally start with a ’−’ sign, followed by a base indicator, where b means the number is a binary number, o means octal, d means decimal, and h means hexadecimal. d n d n−1 ...d 1 d 0 are the digits of the number.

The final argument, output base, gives the base that the resulting number should be printed out in. Like the base indicator for the input numbers, this argument can be one of four strings: “b” for binary, “o” for octal, “d” for decimal, and “x” for hexadecimal. Your program should output the answer in the same form as the input numbers (that is, the output number should follow the regular expression given above).

Some examples:

$ ./calc + b1101 b1 d

d14

$ ./calc + d999999999 d1 d

d1000000000

$ ./calc - d10 -d4 b

b1110

$ ./calc + -d10 -d4 b

-b1110

Important: You must write the base conversion code yourself. You may not use type-casting, libraries, or output formats in printf(). You may use standard C arithmetic operations. You may use the C standard libraries for functionality not related to the conversion (e.g., string handling functions). Important: If calc detects an error in the inputs, it should print out an error message that starts with the string “ERROR”, followed by a string that gives an informative message about the error that it detected.

3 Format Interpretation

Implement a program called format with the following usage interface:

format [input bit sequence] [type]

The first argument, , is a sequence of 32 bits. Remember that your C program will get it as a string of 1 and 0 characters in the argv[1] argument to main. This sequence of bits represents the binary values stored in 4 contiguous bytes in memory. The leftmost bits are stored in the byte with the smallest address while the rightmost bits are stored in the byte with the largest address.

The second argument, , gives the type that you should use to interpret the input bit sequence, and can be either int (integer) or float.

The formats for the input bit sequence is as follows. If the type is:

int: the format is two’s complement;

float: the format is IEEE 754 single precision;

Note that the input bit sequence can correspond to negative numbers.

Your program should print out the decimal representation of the input bit sequence, assuming a big endian byte ordering. Floating point numbers should be printed in scientific notation, where a number 1.5x10 5 would be printed as 1.5e5. For positive infinity, output pinf, for negative infinity, output ninf, and for “NaN”, output NaN.

Here are some examples:

$ ./format 01000001010000100100001101000100 int

1094861636

$ ./format 10000001010000100100001101000100 int

-2126363836

$ ./format 01000001010000100100001110000100 int

1094861700

$ ./format 00000000000000000000000000000001 int

1

$ ./format 01000000110000110100001111010100 float

6.10203e0

$ ./format 00111010000111111111011000001000 float

6.1e-4

$ ./format 10000000000000000000000000000000 float

-0.0e0

$ ./format 01000000010010010000111111011011 float

3.141593e0

Important: You must write the interpretation code yourself. You may not use type-casting, libraries, or output formats in printf(). You may use standard C arithmetic operations. You may use the C standard libraries for functionality not related to the value interpretation (e.g., string handling functions).

Important: If format detects an error in the inputs, it should print out an error message that starts with the string “ERROR”, followed by a string that gives an informative message about the error that it detected. For this program, you can assume that any input bit sequence that is shorter or longer than 32 bits is erroneous.

You'll get 1 file (6.9KB)