CS1500 Algorithms and Data Structures for Engineering, FALL 2012

 LAB 6:Binary representation

In this assignment you are to implement 4 functions that convert integer/double to binary representation (string of 0/1-s) and viceversa. While you can figure this out from the representation algorithm(here, here, here) in this lab you are simply required to write some input in memory, and then read it to get a different representation. For example: to convert a stream of 64 bits to double, you can write the appropriate address of memory with the 0/1 bit values, then tell C++ to interpret the  64 bits as a double. It will do the conversion itself.

A) int convert_char_to_int(char*)
Read a string of bits : validate that each character is a either 0 or 1, and also validate that the string has exactly 32 chars in it (in position index=32 you should have the value of 0, which marks the end of the string). Interpret the sequence of bits as an 32-bits signed integer,  and return the integer number equivalent.
8        = 00010000 00000000 00000000 00000000
261    = 10100000 10000000 00000000 00000000

B) double convert_char_to_double(char*)
Same as before, but this time it is a double representation (mantissa and exponent). Interpret the sequence of bits as an double number on 64 bits,  and return the decimal number equivalent.

C) char* convert_int_to_char(int)
Conversely, take an integer and output its 32-bit representation as as string of 32 characters, each 0 or 1. Make sure your function works properly for negative numbers.
Hint: use the shift operator (>>).

D) char* convert_double_to_char(int)
Conversely, take a double and output its 8bytes=64-bit representation as as string of 64 characters, each 0 or 1. The bit representation is the one using mantissa and exponent.

HINT:You can  implement first the following two functions:
char* read_the_bits(char* x, int size)to read bits from a given address
void write_the_bits(char* x, void* add, int size) to write the given bits at a given address
A good idea is to set size=8, meaning read/write 8 bits at a time.