Homework 1: Bit Fiddling
This HW will give you a chance to practice using binary and bit-wise operators. You’ll likely find Booleans a useful reference.
Task
SSH into the CS portal (See Lab 1 for details). Complete the four problems listed below. For each problem, compose a solution file problemX.bits that contains lightweight code using just operators and assignments like the following:
x = 0x20
y = b + x
That is, set x to be the hex value 0x20, then set y to be the value of b plus x.
The goal is to end up with one variable having a particular value, as defined in the instructions, based on other variables that are provided with new values in each test case. Do not add conditionals, loops, subroutines, etc.
Once you have composed your first solution, run the cso1bits program to test your solution. For example, to run our solution to the subtract problem, we would run:
cso1bits subtract problem0.bits
This program will automatically log your attempt and your code, so there is nothing else to submit once you achieve 100s on all problems.
Getting Status
You can check your status at any time by running:
cso1bits status
Leaderboard
You may also submit your attempts to the leaderboard by giving yourself a nickname:
cso1bits joinboard "My Nickname"
View the leaderboard for any task using the command, which will show the number of operations and constants used by other students who have joined the leaderboard. You do not need to join the leaderboard to view the leaderboard.
cso1bits leaderboard task_name_here
If you attempt to run
cso1bitsand get acommand not found, then you need to add our coursebindirectory (a directory containing programs for our course) to yourPATH. In this case, please follow the steps in Lab 1 to run our course set-up script. You may then need to logout (exit) and SSH back in to get access to thecso1bitsprogram.
The Tasks
We want you to do four of the problems. There are others puzzles on the site as well if you want more practice, but the only four we grade are:
- reversebytes
- Given
x, setyto an endian-swapped version ofx; i.e., reverse the bytes ofx. For example,reversebytesonx=0b00010010_01001000would setyto0b01001000_00010010_00000000_00000000.For full credit, use ≤ 15 operations from {
!,~,+,-,<<,>>,&,^,|} and up to 8-bit constants. - evenbitparity
- Given
x, setyto0if an even number of the even-indexed bits are 1; otherwise, setyto1. (Bit 0 ofxis the 1s place.) For example: for x = 0, y should be 0 (no bits are 1); for x = 2, y should be 0 (all 1 bits are odd-numbered); for x = 3, y should be 1; for x = 5, y should be 0, for x = 21, y should be 1.For full credit, use ≤ 15 operations from {
!,~,+,-,<<,>>,&,^,|} and up to 8-bit constants. - islessorequal
- Given
xandy, setzto1ifx <= y; otherwise setzto0.For full credit, use ≤ 24 operations from {
!,~,+,-,<<,>>,&,^,|} and up to 1-bit constants.. - bitcount
- Given
x, setyto the number of bits inxthat are1.For full credit, use ≤ 40 operations from {
!,~,+,-,<<,>>,&,^,|}.
In all of the tasks, variables are 32-bit signed integers.
Examples
We will do one example in class when we reach the HW1 material. However, if you’d like to practice beforehand, here is the description of subtract:
- subtract (in-class example)
- Given
xandy, setztox - ywithout using-or multi-bit constants.For full credit, use ≤ 10 operations from {
!,~,+,<<,>>,&,^,|} and up to 1-bit constants..
Lab
In Lab 3, we will ask you to do the following example, working with the people at your table. We’ll include it here for now, but please wait until lab to begin so that you’ll have better discussions with your table.
- isequal
- Given
xandy, setzto1ifx == y; otherwise setzto0without using==or multi-bit constants.For full credit, use ≤ 5 operations from {
!,~,+,<<,>>,&,^,|} and up to 1-bit constants.
Collaboration
This Homework ONLY: You may work with other students in this class on this assignment, but only in the following two ways:
-
You worked together from the beginning, solving the problem as a team, with each person contributing.
Each teammate should cite this in each problem with a C-style comment at the top of each solution and also cite the originator of any single-person contributions where they appear, like
// Part of a team with mst3k and jh2jf x = -y w = -x // jh2jf came up with this line z = x + y -
You helped someone with a task you’d already finished, helping them think through their incorrect solution and not giving them or trying to lead them to your solution.
The helper should acknowledge they did this by returning to their previously-submitted solutions and re-submitting them with an added comment at the top, like
// I helped tj1a x = -y w = -xThe helpee should acknowledge they got this by adding a comment at the top, like
// tj1a helped me x = -y w = -x
In all cases, include computing IDs in your citations to streamline our automated tools that assist with collaboration checking.
Hints
If needed, we have some hints you can look at.
subtract
Consider the definition of two’s compliment.
isequal
Consider an operation that results in a single bit value.
reversebytes
The obvious solution to mask the upper byte of x won’t work (0xFF000000 & x) since we are limited to 8-bit constants. How could you effectively do this with only 8-bits?
bitcount
The obvious solution would be something like
ans = 0;
for(int i=0; i<32; i+=1) {
a += x&1;
x >>=1;
}
We don’t allow for loops, but even if you replace it with 32 copies that’s still 96 operations, and we only allow 40 for this task.
The trick is to do things in parallel, treating a number like a vector of smaller numbers. Suppose I wanted to count the bits of an 8-bit number with bits abcdefgh. With a little shifting and masking I could make three numbers
0b00e00h
0a00d00g
0000c00f
and add them to get xx0yy0zz where xx = a+b, yy = c+d+e, and zz = f+g+h.
Extending this trick to several rounds on 32 bits will solve this problem.