Question
This lab consists of a set of puzzles that work with bit-level representations and manipulations, and twos complement representation. The rules for completing lab include:
This lab consists of a set of puzzles that work with bit-level representations and manipulations, and twos complement representation. The rules for completing lab include:
All of these problems can be done with the eight operators:
! ~ & ^ | + << >>
and some require you to stick with just a subset of these.
You are also limited to constants with length at most 8-bits
i.e. you are limited to constants with two hexadecimal digits.
You must use straight-line code no loops or conditionals.
Each puzzle also has a rating of 1 (easiest) to 4 (hardest).
There is a limit on the number of operations you may use (just to eliminate brute force solutions).
Each puzzle is worth 5 points
Sample Puzzles
Sample Puzzle One - tmin - return the minimum two's complement integer
Legal ops: ! ~ & ^ | + << >>
Max ops: 4
Rating: 1
The following code solves (& tests) this function.
#include
int tmin(void) {
return 1<<31;
}
int main() {
printf(Tests for sample puzzle one );
int x;
x = tmin();
printf(Tmin Decimal format %d , x);
printf(Tmin Hexadecimal format %x , x);
printf( );
return 0;
}
For Problem 8 is that right?
Puzzle Eight isLess - if x < y then return 1, else return 0
Example: isLess(4,5) = 1.
Legal ops: ! ~ & ^ | + << >>
Max ops: 30
Rating: 3
isLess(int x, int y) {
return ((((x+((~y)+1))>>31)+1);
}
Problem 9 I need help with
Puzzle Nine threeFourths - multiplies by 3/4 rounding toward 0,
Should exactly duplicate effect of C expression (x*3/4), including overflow behavior.
Examples: ezThreeFourths(11) = 8
ezThreeFourths(-9) = -6
ezThreeFourths(1073741824) = -268435456 (overflow)
Legal ops: ! ~ & ^ | + << >>
Max ops: 12
Rating: 3
int threeFourths(int x) { }
For eleven I also need help with (no loops)
Puzzle Eleven bitCount - returns count of number of 1's in word
Examples: bitCount(5) = 2, bitCount(7) = 3
Legal ops: ! ~ & ^ | + << >>
Max ops: 40
Rating: 4
int bitCount(int x)
------------------------------------------what I thought of
{
int counter;
for(counter =0; value!=0; counter ++, value & = value-1);
return counter;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started