Lab 11: Bitwise Operators
Due: Wednesday, Nov. 12, 2014 @ 11:59 p.m.
Description:
In this lab you will write a program that will exercise two functions, setlsbs() and getlsbs(). These two functions will be used in Programming Project #3.
Background Preparation:
Specifications:
Your first function:
void setlsbs(unsigned char *p, unsigned char b0)
will take as parameters, an array p of eight bytes (unsigned char) and a byte b0. It will replace the least significant bits (LSBs) of p by the bits of b0. In other words, if the binary representation of b0 is b7b6b5b4b3b2b1b0, you should replace the LSB of p[0] by b0, the LSB of p[1] by b1, ... , and the LSB of p[7] by b7.
Your second function:
unsigned char getlsbs(unsigned char *p)
will take an array p of eight bytes (unsigned char) and return a byte b0 which is created by combining the LSBs of p. That is, your function should combine the least significant bits bi of p[i] to return a byte b7b6b5b4b3b2b1b0.
Write a program to test your functions as follows:
You will create a Makefile to compile and link your programs.
Macros:
Use the following macros to print the binary representation of unsigned character variables:
#define BYTETOBINARYPATTERN "%d%d%d%d%d%d%d%d"
#define BYTETOBINARY(byte) \
(byte & 0x80 ? 1 : 0), \
(byte & 0x40 ? 1 : 0), \
(byte & 0x20 ? 1 : 0), \
(byte & 0x10 ? 1 : 0), \
(byte & 0x08 ? 1 : 0), \
(byte & 0x04 ? 1 : 0), \
(byte & 0x02 ? 1 : 0), \
(byte & 0x01 ? 1 : 0)
#define PRINTBIN(x) printf(BYTETOBINARYPATTERN, BYTETOBINARY(x));
You can use the macros in a manner similar to the code below:
unsigned char num = 173;
PRINTBIN(num); printf("\n");
Submission:
Log in to mason or zeus and copy your lab's files into a directory named Lab11. Start a typescript session in the directory above Lab11. Type "uname -a" to show that you are on mason or zeus. Change to the Lab11 directory and use the ls and cat commands to list all your source files and Makefile. Compile your program using the make command. Run your program using 10 different random seeds to show that it executes correctly.
Change back to the directory immediately above Lab11, and use the tar command to create an archive of all the files in the Lab11 directory. Name this archive Lab11.tar. Re-read the man page if necessary to discover the proper options to use.
Finally, exit the typescript session by typing crtl-d.
Verify that your script file was created correctly by using the more (or less) command (your choice) to see the contents of the file. As always, information on these commands can be obtained by using the manual pages ("man more" or "man less"). Once you are sure the file is correct, copy the script file and Lab11.tar back to your local computer and submit both of them to Blackboard as Lab 11.