The main purpose of this assignment is to practice bit manipulation. It doesn't count towards your credit for the unit. You may need information from the lecture notes, especially the bits chapter.
The idea is to write some functions which manipulate bits.
unsigned int colour(int r, int g, int b, int a); void components(unsigned int c, int rgba[4]); unsigned int point(int x, int y, int z); void coordinates(unsigned int p, int xyz[3]); void binary(int n, char s[33]); void hex(int n, char s[9]);
The colour
function packs four colour components (red, green,
blue, 'alpha' = opacity) into a single unsigned integer. The
components
function unpacks a colour into an array of its
components.
The point
function packs three signed coordinates into a 3D
position, held in an unsigned integer. The coordinates
function
unpacks a position into an array of its three signed coordinates. The
coordinates are in the range -512..511
so that they can be
represented using ten bits each.
The binary
function converts the integer n
into a
binary string consisting of 32 digits, each '0'
or
'1'
, in the array provided, which must be at least 33 characters
long to allow for a null character at the end. The hex
function
similarly converts n
into an 8-hex-digit string.
Here's the usual skeleton program and Makefile, to get you started and to provide the tests.
And here are some notes about it:
The program can be used by typing ./bits
to run the tests. But
it can also be used by typing ./bits -h n
to print out the number
n
in hex, and by typing ./bits -b n
to print out the
number n
in binary.
The problem of printing a number in binary, for example, is solved by the two
functions binary
and printBinary
. The first deals
with the logic of the problem (converting a number into binary text) and the
second deals with the input/output (printing the result text). This allows the
logic function to be tested easily, while keeping the I/O function simple so
that it doesn't need to be tested.
The components
, coordinates
, binary
,
and hex
functions have an array passed to them for them to fill
in the answer. The functions would be prettier if they returned the answer, but
arrays can't be returned from functions (without using pointer notation). An
alternative would be to define suitable structs to use instead of arrays, but
the extra complexity doesn't seem worth it.
The testComponents
function is reasonably short and neat, with
only one line per test. To achieve that, an extra function
checkComponents
has been added. The same applies to the other
functions which take array arguments. In general, you should use your normal
programming skills to make testing functions short and neat, just as much as any
other functions in your programs.
In checkComponents
, for example, the array passed to the
components
function is first filled with incorrect characters.
Then, if the components
function fails to fill in one of the
characters, the test will detect that and fail (otherwise the random content
might accidentally be correct, causing the test to succeed when it
shouldn't).
All the details you need, to do with shifting, masking, packing and
unpacking, are described in the bits chapter. You can use standard
functions if they help. For binary
and hex
, you can
use a right-to-left loop (often shown in tutorials because it works well if you
don't want leading zeros) or a left-to-right loop (arguably logically simpler in
this case, where leading zeros are included.)
As well as running the tests, you should also make sure you are familiar,
roughly, with what various numbers, including negative ones, look like in both
binary and hex. You can do that by running ./bits -h n
to display
n
in hex, or ./bits -b n
to display n
in
binary.
As usual, you can develop or start a program of your own choice, perhaps
involving arrays or strings or bit manipulation. Don't continue a program
submitted alongside the triangle assignment, start something new. Submit your
program and a readme.txt
. It will count for the other 50% of this
week's assignment.
Submit bits.c
and your extra work, if any.