3. Digital Logic

3.1. Purpose

This activity is intended introduce and/or reinforce the use of digital logic in a program, as well as further progress a student’s C programming skills.

3.2. Hardware and Tools

  • MSP-EXP432P401R Launchpad Development Board, OR

  • TI-RSLK Robotic Car [CCS Option]

OR

  • None [Online Option]

3.3. Instructions

You have two options for completing this Activity:

  1. [Preferred] Perform all steps within CCS; which will require being connected to a Launchpad Board or RSLK, or

  2. Perform all steps within an online (or local) C IDE/compiler. This will not require hardware but will of course be a different environment than CCS.

3.3.1. CCS Option

  1. Download the template CCS project from here: TemplateProject.zip, and import it into CCS.

  2. As you will be using this project multiple times throughout the course, it is best to rename the project so you may import it again in the future. To do so, right click on the project name in the Project Explorer pane and select Rename…. Select a new project name that makes sense to you.

  3. Continue the activity with the Shared Instructions.

3.3.2. Online Option

  1. Open a new online C IDE/compiler instance. It is suggested to start from the standard “Hello World” program that is the default for many of these:

    #include <stdio.h>
    int main()
    {
    printf("Hello World!");
    }
    
  2. Continue the activity with the Shared Instructions.

3.3.3. Shared Instructions

  1. Create four variables (type: uint8_t or unsigned char) in your program and name them a, b, c, and d. These can be either global or local variables (within main).

  2. Set the initial value of the variables as such:

    a = 0x80;
    b = 0x1E;
    c = 0xC3;
    
  3. For the following expressions, perform the calculation by hand (result in hexadecimal or True/False) and then enter it into the program to check your result. Make sure to print the resulting values as hexadecimal (%x) with the 0x hex identifier. Record both the hand calculations and program outputs on a piece of paper. Note that both the hand calculation results and program output should match. If they do not match, determine which instance is incorrect and why. A description of the operators may be found here: Operators.

    Warning

    Make sure to evaluate the commands required and save the value into the variable d. In certain cases, inserting the expression directly into a printf() statement will not return the expected results. This is due to the fact that printf() will assume the evaluation is of type uint32_t or int32_t instead of the desired uint8_t.

    1. What is the value of d after each expression below?

      Expression

      Hand Calculation

      Program Output

      d = a & b;

      d = b & c;

      d = b & 0xF0;

      d = c & 0x01;

      d = c & ~b;

      d = c & !b;

    2. Repeat all of the above except using bitwise OR instead of AND.

      Expression

      Hand Calculation

      Program Output

      d = a | b;

      d = b | c;

      d = b | 0xF0;

      d = c | 0x01;

      d = c | !b;

    3. What is the output of the operation below? To test in the program, you may select any applicable variable to save into and print.

      Expression

      Hand Calculation

      Program Output

      b << 1

      b << 4

      b << 8

      b >> 1

      b >> 4

    4. Does the operation below result in a TRUE or FALSE? Also record the numerical result of the operation. Remember that any value other than 0 is TRUE and only 0 is FALSE.

      Expression

      Hand Calculation

      Program Output

      TRUE/FALSE

      ~b

      !b

      !(!b)

      a && b

      b && c

      a || b

      (a + b) == (a | b)

      ~b != b

  4. Take a picture(s) of your completed tables and submit to the applicable Gradescope assignment. The code does not need to be uploaded.