Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The rules for completing lab include: All of these problems can be done with the

ID: 3665808 • Letter: T

Question

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

Assignment:

Puzzle Six – byteSwap - swaps the nth byte and the mth byte

Examples: byteSwap(0x12345678, 1, 3) = 0x56341278

                     byteSwap(0xDEADBEEF, 0, 2) = 0xDEEFBEAD

You may assume that 0 <= n <= 3, 0 <= m <= 3

Legal ops: ! ~ & ^ | + << >>

Max ops: 40

Rating: 2

int byteSwap(int x, int n, int m) { }

Puzzle Seven – isPositive - Return 1 if x>0, return 0 otherwise

Difficulty Rating - 3

Legal Operators: ! ~ & ^ | + << >>

Max Ops: 12

isPositive(int x) { }

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) { }

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) { }

Puzzle Ten – bang - Compute !x without using !

Examples: bang(3) = 0, bang(0) = 1

Legal ops: ~ & ^ | + << >>

Max ops: 20

Rating: 4

int bang(int x) { }

Puzzle Eleven (Extra Credit) – bitCount - returns count of number of 1's in word

Examples: bitCount(5) = 2, bitCount(7) = 3

Legal ops: ! ~ & ^ | + << >>

Max ops: 40

Rating: 4

Explanation / Answer

Exercise 6:

int byteSwap(int x, int n, int m)

{

int n1=0;

int m1=0;

int n2,m2;

n<<=3;

m<<=3;

n1= x<<n;

m1=x<<m;

n2=x & n1;

m2=x & m1;

n2 >.=n;

n2<<=m;

n2&= m1;

m2 >>=m;

m2<<=n;

m2 &=n1;

x &= ~(n1|m1);

x | =(n2|m2);

return x;

}

Exercise 7:

int isPositive(int x)

{

int y;

y= 1<<31;

y= x&y;

y= y| !x;

return y;

}

Exercise 8:

int isLess(int x, int y)

{

return ((((x+((~y)+1))>>31)+1);

}

Exercise 9:

int bang(int x)

{

int inverseOfX =~x;

int negationX = inverseOfX+1;

int y;

y= (~negationX & inverseOfX)>>31;

y = y &1;

return y;

}

int bitCount(int x)

{

int counter;

for(counter =0; value!=0; counter ++, value & = value-1);

return counter;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote