Name:

Date:

 

Lab 2 - Displaying Binary, Decimal and Hexadecimal



Introduction 

Due

Lab Steps

Part 0 – Input and Output in C

  1. If you are unfamiliar with the C functions scanf and printf, read this C input/output tutorial. Pay careful attention to the use of the address-of operator & with the scanf function.
  2. Try out each example in the tutorial. To do so, begin by logging in 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. Clear your directory structure rooted at csc8400 created in the previous lab (but keep the directory csc8400). In your csc8400 directory, create another directory called intro, then change your current directory to csc8400/intro.
  3. Use a text editor of your choice (pico, vi, emacs) to write your programs. 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 io.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.

4.     Type in the first C program, and make sure it compiles and runs correctly. To compile your code, use the command

      gcc io.c -o io 

gcc (which stands for GNU Compiler Collection) is the name of the compiler. The first argument io.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 io) 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, however keeping the executable name the same as the program name (without the .c extension) is a good rule of thumb, so you can easily associate an executable with the corresponding source code. Pay careful attention to error and warning messages, and eliminate them all.

5.     To execute your program, use the command

         ./io 
  1. Repeat for each program in the tutorial. Ask for assistance if you find anything unclear.

 

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.  In your csc8400 directory create another directory called binary, then change your current directory to csc8400/binary.
  2. Create a file called conversion.c using a text editor of your choice (pico, vi, emacs). If you are not familiar with any of these editors, please refer to Part 0 for guidelines on using the pico editor.  
  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;
                for (i = 0; i < 32; i++)
                {
                      // print 2 raised to power i in here
                }
          }

     
  4. Invoke your powers_of_2 function in the main function.
  5. Compile your code with gcc and execute it (refer to Part 0 for instructions on how to do this). Pay careful attention to error and warning messages, and eliminate them all.
  6. Demonstrate your program and have the instructor initial here: _______

Part 2 - Convert Decimal to Binary

  1. Add another function to your conversion.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 same position as in mask is 1
}
else
{
    // bit in same position as in mask is 0
}

  1. Think about how you could use such a "mask" to find the value of any bit. Perhaps using the LEFT SHIFT operator (<<) 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 Binary to Decimal

  1. Add another function to your lab3.c file 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;
          int numdigits = strlen(value);
          for (i = 0; i < numdigits; i++)
          {
                printf("binary digit %d is: %c\n", i, value[i]);
          }

     
  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: _______

Part 4 – Command Line Arguments

C, C++ and Java allow you to input values into your program through the command line. You will only need to declare the main function in a slightly different form. In C (and C++), this is

        int main( int argc, char * argv[] )

Here argc is the number of command line arguments (including the name of the executable), and argv is an array of the arguments.

  1. Write a program called cmdargs.c that includes the following main function:

          int main( int argc, char * argv[] )

          {        

              int i;       

 

              printf("\nYour program name is %s\n", argv[0]);        

              printf("Your arguments are: \n");        

              for(i = 1; i < argc; i++)            

                 printf("\t %s starts with %c\n", argv[i], argv[i][0]);

              return 0;    

          }

2.     Save your program and compile it using the command

                        gcc cmdargs.c -o cmdargs

Pay careful attention to error and warning messages, and eliminate them all. To execute your program, use the command

         ./cmdargs let us see what this does

           Inspect the output of your code and analyze the code to answer the following questions:

a)      What data type is argc?

b)     What value does argc take when the program gets invoked? How is it related to the command line you used to invoke the program?

c)      What data type is argv?

d)      What data type is argv[0]? What about argv[1]?

e)      What data type is argv[0][0]? What about argv[0][1]?

f)      What gets stored in argv[0]? What gets stored in argv[1]?

g)     What values are stored in argv when the program gets invoked? Draw a diagram (memory boxes and arrows for pointers) illustrating the memory space occupied by argv.

 

Part 5 – Reading from the Command Line

  1. Make your program accept an integer value on the command line and feed that value into your decimal_to_binary function instead of "hard coding" it. For instance, you would run your program like this, and get the following output:
          ./conversion 21
          00000000000000000000000000101010
  2. Demonstrate your program and have the instructor initial here: _________

 

Part 6 – Optional (Extra Credit)

  1. Make your functions return a value rather than printing it out. Then pass the return value of one function into the other. For example:

          char *binary = "00000000000000000000000000101010";
          int decimal = binary_to_decimal(binary);
          char *newbinary = decimal_to_binary(dec);
          printf("old binary: %s\n", binary);
          printf("new binary: %s\n", newbinary);

     
  2. Demonstrate your extra credit features and have the instructor initial here: _________