Bit Fiddling
This HW will give you a chance to practice using binary and bitwise operators. You’ll likely find Booleans a useful reference.
Task
Visit https://kytos02.cs.virginia.edu/cso1/bitfiddle.php and complete the four problems listed below. The text boxes want lightweight code using just operators and assignments, like
x = 0x20
y = b + x
The goal is to end up with one variable having a particular value, based on other variables that are provided with new values in each test case. Do not add conditionals, loops, subroutines, etc.
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:
 subtract
 Given
x
andy
, setz
tox  y
without using
or multibit constants.For full credit, use ≤ 10 operations from {
!
,~
,+
,<<
,>>
,&
,^
,
}.  bottom
 Given
b
, set the loworderb
bits ofx
to 1; the others to 0. For example, ifb
is 3,x
should be 7. Pay special attention to the edge cases: ifb
is 32x
should be −1; ifb
is 0x
should be 0. Do not use
in your solution.For full credit, use ≤ 40 operations from {
!
,~
,+
,<<
,>>
,&
,^
,
}.  anybit
 Given
x
, sety
to1
if any bit inx
is1
; sety
to0
ifx
is all0
s.For full credit, use ≤ 40 operations from {
~
,+
,
,<<
,>>
,&
,^
,
}.  bitcount
 Given
x
, sety
to the number of bits inx
that are1
.For full credit, use ≤ 40 operations from {
!
,~
,+
,
,<<
,>>
,&
,^
,
}.
Collaboration
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 Cstyle comment at the top of each solution and also cite the originator of any singleperson 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 previouslysubmitted solutions and resubmitting them with an added comment at the top, like
// I helped tj1a x = y w = x
The 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.
bottom
The obvious solution ~(0xFFFFFFFF << b)
won’t work. Bit shifts always do a modulo on their righthand operand, so a << b
does the same thing as a << (b % (8*sizeof(a))
. Thus, << 32
and << 0
do the same thing.
anybit
The easy solution would be y = !!x
but we don’t allow !
. Nor do we allow enough operations to do a looplike solution.
You can divide and conquer. Try defining x1
where if any bit anywhere in x
was 1
, some bit in the bottom 16 bits of x1
is 1
. The given task is “see if any 1
bit is in the 32 bits of x
”. How could you reduce it to “see if any 1
bit is in the bottom 16 bits of x1
”?
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 8bit 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.