Advanced C Programming

Summer 2022 ECE 264 :: Purdue University

⚠ This is a PAST SEMESTER (Summer 2022).
Due 8/5

Bitwise operators

Learning goals

You should learn:

  1. Practice using bitwise operators.

Overview

In this homework, you will practice using binary operators to set and unset bits in a byte. It is important that you understand all six bitwise operators to be able to complete this homework. They are in your reference sheet as well as the relevant lecture.

Getting Started

This homework will not use or interact with any of the files from the earlier homeworks.

  1. Run 264get EC03 to fetch the starter code.

    We have provided a skeleton file (bitwise.c) and a header file (bitwise.h) to save you time. Do not let this discourage you from using miniunit to test if you want more verification.

    You will also find two other files (print_bits.o and print_bits.h). See the Q&A for more details.

  2. Change into your newly created EC03 directory.
    you@ecegrid-thin1 ~ $ cd ec03
  3. Copy in your Makefile, clog.h, and miniunit.h from HW15 (or any other homework).
    you@ecegrid-thin1 ~/ec03 $ cp ../hw15/{Makefile,clog.h,miniunit.h} -t ./
  4. Update your Makefile.
    you@ecegrid-thin1 ~/ec03 $ vim Makefile
    • Set ASG_NICKNAME to EC03.
    • Set SRC_C to bitwise.c print_bits.o and TEST_C to test_bitwise.c.
    • Set SRC_H to bitwise.h miniunit.h clog.h print_bits.h.
    • Ensure that your submit rule includes print bits. Altogether, it should have the following: $(SRC_C) $(SRC_H) $(TEST_C) Makefile

Requirements

  1. Your submission must contain each of the following files, as specified:
    file contents
    bitwise.c Functions are specified in the comments, in bitwise.c or bitwise.h.
    test_bitwise.c functions
    main(int argc, char✶ argv[])
    return type: int
    • 100% code coverage is required for bitwise.c.
    • Some tests using print_bits.h are provided as a start.
    • Feel free to expand those tests and to use your miniunit.h if you wish.
    bitwise.h Do not modify.
    miniunit.h
    as usual
    clog.h
    as usual
    Makefile
    as usual
  2. When the final byte is written, any unused bits should be filled with 0's on the least significant (right) side.
  3. Do not call any file operations anywhere within your bitwise.c, except as specified below. These restrictions are for your protection.
  4. The following external header files, functions, and symbols are allowed.
    header functions/symbols allowed in…
    stdbool.h bool, true, false *
    stdio.h printf, fprintf, fputs, fputc, stdout, fflush bitwise.c, test_bitwise.c
    assert.h assert *
    stdlib.h EXIT_SUCCESS, size_t bitwise.c, test_bitwise.c
    stdint.h uint8_t bitwise.c, test_bitwise.c
    string.h * bitwise.c, test_bitwise.c
    miniunit.h * test_bitwise.c
    clog.h * bitwise.c, test_bitwise.c,
    “*” means anywhere (i.e., any file) or anything (i.e., any function, constant, type, etc.). All others are prohibited unless approved by the instructor. Feel free to ask.
  5. Submissions must meet the code quality standards and other course policies on homework.

Submit

To submit EC03 from within your ec03 directory, type

make submit

That should result in the following command: 264submit EC03 bitwise.c bitwise.h test_bitwise.c miniunit.h clog.h Makefile

Pre-tester

The pre-tester for EC03 has been released and is ready to use.

Q&A

  1. How much work is this?

    This assignment was originally designed as the warmup for last semesters binary file assignment. Since this semester is using HW16 for its binary file assignment, I adopted the warmup as a standalone assignment just to make sure you got a chance to work with bitwise operators.

    If you joined or viewed the relevant lecture(s), and followed them, this homework should be easy, probably about an hour of work. However, if you skipped them, skimmed, or didn't pay proper attention, then you will almost certainly need to go back and watch them. Searching for posts on Stackoverflow isn't going to help you much. (That said, if you do find any great resources, please share them on Piazza!)

    As a reminder, there are 6 bitwise operators that you will use here: ^ << >> & | ~. (They are also summarized on the reference sheet, but you need to internalize how they are used.

    The warmup will require writing exactly 14 sloc. There are 11 functions that must be implemented in exactly one statement. One more function must be implemented by adding exactly three statements.

  2. I think I understand bitwise, but how do I start?

    First, do the Getting Started above.

    Next, just choose any function that you know how to do. They should be able to be completed in any order.

    As you finish more functions, it will hopefully become more clear how to do the rest of the functions. If you get stuck, feel free to ask on piazza or in office hours. ertions.)

  3. How am I supposed to convert all of these binary bits to hexadecimal in my head?

    It's not as hard as it looks at first. Each 4 bits is a “nibble”, and is represented by one hex digit. There are only 16 hex digits.
    decimal binary hex
    0 0000₂ 0
    1 0001₂ 1
    2 0010₂ 2
    3 0011₂ 3
    4 0100₂ 4
    5 0101₂ 5
    6 0110₂ 6
    7 0111₂ 7
    8 1000₂ 8
    9 1001₂ 9
    10 1010₂ a
    11 1011₂ b
    12 1100₂ c
    13 1101₂ d
    14 1110₂ e

    So if you see 00011001₂, break it into 0011₂ and 1001₂. If you don't remember the table above, then it's not hard to interpret a 4-digit binary number in your head. Just remember the places are 1, 2, 4, and 8 (right-to-left). For the first one, you have 1 + 2 = 3. In hex, that is 0x03 (easy!). For the second one, you have 1 + 8. In hex that is 0x09 (also easy!).

    If you get 1111₂, that's a good one to remember; it's f.

    From there, 1110₂ is just one less, so it's e.

    That leaves 1010₂ (a), 1011₂ (b), 1100₂ (c), and 1101₂ (d). For those ones, you might need to count the letters on your fingers or something, but with a little practice, this will quickly become second nature.

  4. What are the files print_bits.o and print_bits.h?

    They contain some functions for printing bits to the terminal that help you see if your code is correct.
  5. Why does print_bits.o end with .o?

    It is an object file. (This was covered in miniunit) and the related lecture, but you can be forgiven if you forgot. An object file contains compiled functions, but is not executable on its own. When compiling another program, you can link in the object file to gain access to its functions. This is much simpler than it sounds. When you call gcc, simply add print_bits.o to the command. For example, when you compile , you use this command:
    gcc -o test_bitwise test_bitwise.c bitwise.c print_bits.o
  6. Why didn't you just give us the .c file?

    One of the functions in print_bits.c/print_bits.o is essentially the same as the one you need to write at the end of the warmup. We didn't want to give it away. However, having that function makes the earlier ones easier since you can see what you're doing.
  7. May I use the print_bits_color(…) and/or print_bits_plain(…) functions in my final test_bitwise.c?

    Yes. While we always encourage using miniunit.h for testing, this homework is pretty simple so tests using print bits is sufficient (as long as you get full code coverage). Simply add print_bits.o to the end of each of your gcc commands in your Makefile. Treat it as if it were a .c file.
  8. How can I see what's inside an object (.o) file?

    The objdump command can be useful. Google it.
  9. That sounds complicated. How can I see what functions are available and how to use them?

    See the header file (print_bits.h). Header files often (but not always) serve as a sort of documentation.

Updates

▒▒▒▒▒▒▒▒▒ ▒▒▒▒▒▒▒▒▒