Name:

Date:

 

Lab 3 - Displaying Binary, Decimal and Hexadecimal



Introduction 

Due

Worth

 

Lab Steps

Part 0 – C Input and Output Refresher

  1. If you need to refresh your knowledge of scanf and printf, read this C input/output tutorial. Pay careful attention to the use of the address-of operator & with the scanf function.

Part 1 - Powers of 2

  1. Login to one of the department's Unix or Linux machines, or to the Terminal on a Mac or a Linux virtual machine if you have one already installed.  Unless you have already done so, clear the contents of your csc2400 directory using the command

 rm –r ~/csc2400/*

The symbol star * is a wildcard character that stands for “everything”, so this command reads “remove everything that is in the directory csc2400”. 

  1. In your csc2400 directory, create another directory called binary and then change your current directory to csc2400/binary. Create a file called binhex.c using a text editor of your choice (pico, vi, emacs). If you are not familiar with any of these editors, I recommend starting with pico. This is not the most powerful editor in Unix, but it is the simplest to use. Invoke the pico editor with the name of the file you wish to edit as an argument, as in the command

      pico binhex.c

Notice the menu at the bottom of the editor window that reminds you how to save your code with CTRL-O, how to move between pages with CTRL-V and CTRL-Y, how to cut and uncut a line of text with CTRL-K and CTRL-U, and how to exit with CTRL-X. Remember to save your code frequently.

  1. Create a function that prints the powers of 2 on the screen. Use this function template as a starter:
          void powers_of_2()
          {

int i;

unsigned int ipower;

for (i = 0; i < 32; i++)
   {
       // assign to ipower 2 raised to power i

          // print ipower
            }
      }

  1. Invoke your powers_of_2 function in the main function.
  2. Compile your code and eliminate all warnings and errors. To compile your code, use the command
      gcc binhex.c -o bh 

gcc (which stands for GNU Compiler Collection) is the name of the compiler. The first argument binhex.c is the name of the C program you wish to compile. The option –o informs the compiler that the argument to follow (in our case bh) is the name of the executable program to be produced as result of the compilation. You can give any name you wish to the executable, but make sure to remember it. Pay careful attention to error and warning messages, and eliminate them all.

5.     To execute your program, use the command

         ./bh 
  1. Demonstrate your program and have the TA or the instructor initial here: _______

Part 2 - Convert Decimal to Binary

  1. Add another function to your binhex.c file that prints the full 32-bit binary representation of a decimal integer value. For example, if the value is 25, here is what the call to the function should look like and what the output should be:

     decimal_to_binary(25);
00000000000000000000000000011001

  1. Start with this function template, implementing the part that prints the binary digits.


void decimal_to_binary(int value)
{
    // print the binary representation of value
}

 

  1. Idea: you can tell if a specific bit in a value is 1 or 0 using a "mask" and the bitwise AND operator (&) like this:

unsigned int mask = 0x00000001;
if (value & mask)
{
    // bit in the least significant position is 1
}
else
{
    // bit in the least significant position is 0
}

 

 

  1. Think about how you could use such a "mask" to find the value of any bit. Perhaps using the RIGHT SHIFT operator (>>) to bring the bit you need in the least significant position could help. Perhaps using a loop that goes through each of the 32 bits could help. Explore, experiment, try things.
  2. Make sure that your function correctly handles "signed" (negative) values as well as "unsigned" values.
  3. Add a few calls to this new function inside your main function, passing in various values to demonstrate proper functioning.
  4. Demonstrate your program and have the instructor initial here: _______

Part 3 - Convert Decimal to Hexadecimal

  1. Add another function to your binhex.c file that prints the 8-digit hexadecimal representation of a decimal integer value. For example, if the value is 57, here is what the call to the function should look like and what the output should be:

     decimal_to_hex(57);
00000039

  1. Start with this function template, implementing the part that prints the binary digits.


void decimal_to_hex(int value)
{
    // print the hexadecimal representation of value
}

 

  1. Idea: extract the least significant hexadecimal digit using a "mask" and the bitwise AND operator (&) like this:

 

unsigned int mask = 0x0000000F;

int hexvalue = value & mask;

 

if (hexvalue < 10)

{

    // hexadecimal digit is hexvalue

}

else if(hexvalue == 10)

{

    // hexadecimal digit is A

}

else if(hexvalue == 11)

{

    // hexadecimal digit is B

}

 

and so on.  If you wish to simplify your code, you may place all hexadecimal digits in an array of characters, and use the hexvalue as an index in the array as follows:

 

char hexdigit[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

unsigned int mask = 0x0000000F;

 

int hexvalue = value & mask;

 

// hexadecimal digit is hexdigit[hexvalue]

// print it with printf(“%c”, hexdigit[hexvalue]);

 

  1. Think about how you could use such a "mask" to find the value of any hexadecimal digit. Perhaps using the RIGHT SHIFT operator (>>) to bring the four bits corresponding to the hexadecimal digit in the least significant position could help. Perhaps using a loop that goes through each of the 8 hexadecimal digits could help. Explore, experiment, try things.
  2. Make sure that your function correctly handles "signed" (negative) values as well as "unsigned" values.
  3. Add a few calls to this new function inside your main function, passing in various values to demonstrate proper functioning.
  4. Demonstrate your program and have the instructor initial here: _______

Part 4 - Convert Binary to Decimal [OPTIONAL]

  1. Add another function to your binhex.c file that prints that prints the integer value of any number of binary digits contained in a string. For example, here is a call to this function and what the output should look like:
          binary_to_decimal("00010101");
          21
  2. Start with this function template, implementing the part that prints the binary digits.

          void binary_to_decimal(char value[])
          {
            // print the integer corresponding to the binary "value"
          }

     
  3. Idea: you can tell how many binary digits are in the value using the strlen function, and get each digit, like this:

          int i, digit;
          int numdigits = strlen(value);
          for (i = 0; i < numdigits; i++)
          {
                /* Extract binary digit from character */
                digit = value[i] –
    '0';
                printf("binary digit %d is: %d\n", i, digit);
          }

     
  4. Think about how the position of each binary digit relates to the value the digit in that position represents. Consider how using a loop to go through each of the digits, perhaps going from right to left, might make it possible to sum up the values of all of the binary digits into the integer equivalent. Explore, experiment, try things.
  5. Make sure that your function correctly handles "signed" (negative) binary values as well as "unsigned" ones. You should assume that the most significant bit (furthest to the left) is the sign bit, and if it is a 1 you should incorporate the 2s Complement algorithm into your function.
  6. Add a few calls to this new function inside your main function, passing in various values to demonstrate proper functioning.
  7. Demonstrate your program and have the instructor initial here: _______