Last Updated: 2023-02-14 Tue 09:35

CSCI 2021 Lab05: Shifting and Masking

CODE DISTRIBUTION: lab05-code.zip

CHANGELOG: Empty

1 Rationale

Certain applications utilize bits at an individual level. This is enabled by support for bit-level operations in most processors which are presented in C programs as bit-wise logical and shift operations. This lab explores the basic mechanics of these operations in preparation for project work which will provide a concrete application for them.

Grading Policy

Credit for this Lab is earned by completing the exercises here and submitting a Zip of the work to Gradescope. Students are responsible to check that the results produced locally via make test are reflected on Gradescope after submitting their completed Zip. Successful completion earns 1 Engagement Point.

Lab Exercises are open resource/open collaboration and students are encouraged to cooperate on labs. Students may submit work as groups of up to 5 to Gradescope: one person submits then adds the names of their group members to the submission.

See the full policies in the course syllabus.

2 Codepack

The codepack for this lab is linked at the top of this document. Always download it and unzip/unpack it. It should contain the following files which are briefly described.

File Use Description
QUESTIONS.txt EDIT Questions to answer: fill in the multiple choice selections in this file.
masking.c EDIT C file to edit and complete; TODO sections are marked in the code
Makefile Build Enables make test and make zip
QUESTIONS.txt.bk Backup Backup copy of the original file to help revert if needed
QUESTIONS.md5 Testing Checksum for answers in questions file
test_quiz_filter Testing Filter to extract answers from Questions file, used in testing
test_lab05.org Testing Tests for this lab
testy Testing Test running scripts

3 QUESTIONS.txt File Contents

Below are the contents of the QUESTIONS.txt file for the lab. Follow the instructions in it to complete the QUIZ and CODE questions for the lab.

                           __________________

                            LAB 05 QUESTIONS
                           __________________





Lab Instructions
================

  Follow the instructions below to experiment with topics related to
  this lab.
  - For sections marked QUIZ, fill in an (X) for the appropriate
    response in this file. Use the command `make test-quiz' to see if
    all of your answers are correct.
  - For sections marked CODE, complete the code indicated. Use the
    command `make test-code' to check if your code is complete.
  - DO NOT CHANGE any parts of this file except the QUIZ sections as it
    may interfere with the tests otherwise.
  - If your `QUESTIONS.txt' file seems corrupted, restore it by copying
    over the `QUESTIONS.txt.bk' backup file.
  - When you complete the exercises, check your answers with `make test'
    and if all is well, create a zip file with `make zip' and upload it
    to Gradescope. Ensure that the Autograder there reflects your local
    results.
  - IF YOU WORK IN A GROUP only one member needs to submit and then add
    the names of their group.


QUIZ mask.c
===========

  Study the code in `masking.c' which uses a number of bit-wise
  operations that have recently been discussed in lecture. These
  include:
  ,----
  | | Operation | Description          |
  | |-----------+----------------------|
  | | x & y     | Bit-wise And         |
  | | x | y     | Bit-wise Or          |
  | | x ^ y     | Bit-wise Xor         |
  | | ~x        | Bit-wise inversion   |
  | | x << y    | Bit-wise Left-shift  |
  | | x >> y    | Bit-wise Right-shift |
  `----
  Study the examples provided in `masking.c' and ensure you understand
  their meaning according to the context provided. Then answer the
  following questions.


Terminology
~~~~~~~~~~~

  - "Setting" a given bit means to place a 1 that position; "Set bit 7"
    means to ensure that the 7th bit from the left is a 1
  - "Clearing" a give bit means to place a 0 at that position; "Clear
    bit 7" means to ensure that the 7th bit from the left is a 0


Shift with Or
~~~~~~~~~~~~~

  What is the effect of the following line of code:
  ,----
  | x = x | (1 << 19);
  `----

  - ( ) Changes `x' so that only its 19th bit is a 1, all other bits are
    0.
  - ( ) Changes `x' so that only its 19th bit is a 0, all other bits are
    unchanged.
  - ( ) Changes `x' so that its 19th bit is set to 1, all other bits
    unchanged.
  - ( ) Changes `x' so that its bits are shifted left by 19 places.


Shift with Or with Pattern
~~~~~~~~~~~~~~~~~~~~~~~~~~

  If `x' is cleared (value all 0's), what is the effect of the following
  line of code:
  ,----
  | x = x | (0b110101 << 8);
  `----

  - ( ) Sets the bits of `x' to the given pattern `0b110101' starting
    with the 8th position.
  - ( ) Checks that `x' has the given pattern `0b110101' starting with
    the 8th position.
  - ( ) Copies the given pattern `0b110101' 8 times throughout `x'
  - ( ) Adds 53 on the value of `x' faster than normal addition.


Shift with Invert with And
~~~~~~~~~~~~~~~~~~~~~~~~~~

  What is the effect of the following line of code:
  ,----
  | x = x & ~(1 << 8);
  `----

  - ( ) Changes `x' so that only its 8th bit is a 1, all other bits are
    0.
  - ( ) Changes `x' so that only its 8th bit is a 0, all other bits are
    unchanged.
  - ( ) Changes `x' so that its 8th bit is set to 1, all other bits
    unchanged.
  - ( ) Changes `x' so that its bits are shifted left by 8 places.


Shift with And
~~~~~~~~~~~~~~

  What is the effect of the following line of code:
  ,----
  | if( x & (1 << 13) ){ ... }
  `----

  - ( ) Conditionally execute only if the 13th bit of `x' is 0 and set
    that bit subsequently.
  - ( ) Conditionally execute only if the 13th bit of `x' is 0 but leave
    `x' unchanged.
  - ( ) Conditionally execute only if the 13th bit of `x' is 1 and set
    that bit subsequently.
  - ( ) Conditionally execute only if the 13th bit of `x' is 1 but leave
    `x' unchanged.


CODE Complete masking.c
=======================

  Complete the TODO items in the `masking.c' file so that the missing
  blocks produce the effect mentioned in the `printf()' statements.

  Test that the code behaves correctly via the command
  ,----
  | make test-code
  `----

  and verify that both code/quiz are correct via
  ,----
  | make test
  `----

  before using
  ,----
  | make zip
  `----

  to create a zip to submit.

4 Submission

Follow the instructions at the end of Lab01 if you need a refresher on how to upload your completed lab zip to Gradescope.


Author: Chris Kauffman (kauffman@umn.edu)
Date: 2023-02-14 Tue 09:35