Homework 2

Due: Thu, February 10, 2011, 11:59pm
Submit solutions on Blackboard

Go to http://codepad.org and try out the following simple C Programs. When you visit the site you may choose to create an account that will help you track your work. It is not required to create an account to actually use the site though.

Exercise 0: The obligatory greeting program.
void main(void)
   printf("Hello 9S12");

Exercise 1
: Numbers - Signed vs. Unsigned, 8-bit vs. 16-bit

In this exercise we will see how to define a 8-bit signed or unsigned number. Note that we use char as the type, to declare 8-bit numbers in C. To define a 16-bit number we use short as the type.

void main(void)
   unsigned char u8bit; // Unsigned 8-bit number
   signed char s8bit;   // Signed 8-bit number

   unsigned short u16bit;

   u8bit = 166;

   printf("u8bit as an unsigned number = %u\n",u8bit);
   printf("u8bit in decimal = %d\n",u8bit);
   printf("u8bit in hex = %x\n",u8bit);

   s8bit = u8bit;
   printf("s8bit as a signed number is = %d\n",s8bit);

   u16bit = 0xABCD; // Hex numbers have the 0x prefix
printf("u16bit is = %d\n",u16bit);


printf is a library function you call in C programs to print something out to the screen. You pass it the string you want to output in quotes with placeholders for variables inserted where needed. The placeholders must have the format %d (for decimal), %u (for unsigned decimal), %x (for hexadecimal), %c (for character), %s ( for string) etc. Other special characters like \n (for newline), \t (for tab) can also be embedded in the string.
Try different values for u8bit, s8bit and u16bit for practice.

(Note that in an embedded system we may not have a screen to output to so the printf library call may not exist) 

Exercise 2
: Logical Operations - AND, OR and XOR
In this exercise we will see how to perform simple logical operations.
void main(void)
   unsigned char sw1, sw2, lt; //declare two switches and 1 light
   sw1 = 0x01;
   sw2 = 0x00;
// light ON only of both sw1 and sw2 are ON
   if (sw1 & sw2) {  // Condition check
       lt = 0x01;
       printf("Light On");
   else {
       lt = 0x00;
printf("Light Off");

Try the code with different options for turning the light on:
Exercise 3: Left and Right shift.
In this exercise we wish to check if the 5th bit of a number say anum is the same as the 2nd bit of a second number say bnum. If they are the same then we print out "yes" otherwise we print "no".
void main(void)
   unsigned char anum, bnum;
   anum = 0x2F;
   bnum = 0xF9;
   //Mask out (clear) all the other bits of anum except the 5th bit
   anum =
anum & 0x10;
   anum = anum>>3;  // right shift it by three places

//Mask out (clear) all the other bits of bnum except the 2nd bit
   bnum = bnum & 0x02;

   if (anum == bnum) {
   else {
Try different values of anum and bnum. Also try to write code for the following.

What to Submit?
You will submit the solution to the following exercise - Complete the code below to accomplish the tasks specified as comments. You may declare additional variables if you feel you need them.

void main(void)
     //Declare below a 8-bit unsigned number called foo

    // Declare any additional variables if needed

     //Initialize foo to the hexadecimal value AB

     //Write code (possibly more than 1 line) to swap the
     // lower and upper 4-bits of foo; After executing your
     // statements foo should have the value BA in hex.

     // The following if-then-else statement
     // prints the success or failure of your swap code written above
     if (foo == 0xBA) {
          printf(“Output of Homework 2: Swap Successful”);
      else {
          printf(“Output of Homework 2: Swap Failed");


The output of your code if completed correctly should read:
Output of Homework 2: Swap Successful

Submit a printout of the Codepad screen showing both the code listing and the generated output. You must turn this in before the due date specified above by uploading the screenshot on Blackboard.