Name:

Date:


Lab - Displaying Binary, Decimal and Hexadecimal

Introduction

Note

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.
  2. In your systems directory, create another directory called binary and then change your current directory to systems/binary. Create a file called binhex.c using emacs by typing in at the shell prompt
         emacs binhex.c
    Remember to use Ctrl-x Ctrl-s to save your file, Ctrl-x Ctrl-c to exit, and Ctrl-g to cancel a partially typed or accidental command. Save your code frequently.

  3. 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 the value of 2 raised to power i
                     // print ipower
                  }
          }
    
  4. Invoke your powers_of_2 function in the main function.

  5. Compile your code and eliminate all warnings and errors. To compile your code, use the command
         gcc binhex.c -o xbinhex 
    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 xbinhex) 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 be sure to remember it. Pay careful attention to error and warning messages, and eliminate them all.

  6. To execute your program, use the command
        ./xbinhex 

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, the call
        decimal_to_binary(25);
    should produce the output
        00000000000000000000000000011001
  2. Start with the function template below and implement the part that prints the binary digits:
        
        void decimal_to_binary(int value)
        {
            // print the binary representation of value
        }
    

  3. Idea: you can tell if the least significant 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
            }
    
  4. 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.

  5. Make sure that your function correctly handles "signed" (negative) values as well as "unsigned" values.

  6. Add a few calls to this new function inside your main function, passing in various values to demonstrate proper functioning.

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, the call
        decimal_to_hex(57); 
    should produce the output
        00000039
  2. Start with the function template below and implement the part that prints the binary digits:
    
        void decimal_to_hex(int value)
        {
              // print the hexadecimal representation of value
        }
     
  3. 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]);
     
  4. 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.

  5. Make sure that your function correctly handles "signed" (negative) values as well as "unsigned" values.

  6. Add a few calls to this new function inside your main function, passing in various values to demonstrate proper functioning.

Part 4 - Convert Binary to Decimal [OPTIONAL, 15 bonus points for working code]

  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, the function call
    
          binary_to_decimal("00010101");
    should produce the output value 21.
  2. Start with the function template below and implement the part that prints the integer value:
    
          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.

Submission Instructions

In your systems/binary directory create a text file named readme (not readme.txt, or README, or Readme, etc.) that contains:
  1. Your name(s)
  2. The machine you used to complete the assignment (i.e, tanner or your own machine). If you used your own machine, please be ready to demo your code to your instructor at any time upon request.
  3. A description of whatever help (if any) you received from others outside of class.
  4. An indication of how much time you spent doing the assignment outside of class.
  5. Your assessment of the assignment: Did it help you to learn? What did it help you to learn? Do you have any suggestions for improvement? Etc.
  6. Any information that will help us to grade your work in the most favorable light. In particular you should describe all known bugs.
Descriptions of your code should not be in the readme file. Instead they should be integrated into your code as comments. Your readme file should be a plain text file. Do not create your readme file using Microsoft Word or any other word processor.

Turn in a printout copy of the readme and binhex.c files, and a sample output demonstrating how function works (one printout per team). If you work on the department Unix system, you will need to copy and paste your code into a text or a Word document in Windows, since you cannot print directly from Unix.

Leave the source code for all exercises in your directory systems/binary. Do not make any changes to these files after the due date for this assignment. If you wish to continue working on these exercises after the due date, make a copy of your directory binary using the following Unix command:

    cp -r ~/systems/binary ~/systems/binary-copy
A new directory called binary-copy will be created in your systems directory. You may now make any changes you want to the files in your binary-copy directory, at any time.