Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion .github/workflows/classroom.yml
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,19 @@ jobs:
comparison-method: contains
timeout: 10
max-score: 2
- name: bitReverse
id: bitreverse
uses: classroom-resources/autograding-io-grader@v1
with:
test-name: bitReverse
setup-command: sudo apt-get install gcc-multilib && make clean
&& make all
command: ./btest -f bitReverse
input: ''
expected-output: 'Total points: 2/2'
comparison-method: contains
timeout: 10
max-score: 2
- name: Autograding Reporter
uses: classroom-resources/autograding-grading-reporter@v1
env:
Expand All @@ -282,5 +295,6 @@ jobs:
FLOAT_I2F_RESULTS: "${{steps.float_i2f.outputs.result}}"
ODDPARITY_RESULTS: "${{steps.oddparity.outputs.result}}"
BITCOUNT_RESULTS: "${{steps.bitcount.outputs.result}}"
BITREVERSE_RESULTS: "${{steps.bitreverse.outputs.result}}"
with:
runners: operator-check,signmask,bitxor,clearbyte,roundup,negativepart,islargerorequal,logicalshift,swapnibblepairs,secondlowestzerobit,rotaterightbits,fractions,overflowcalc,mul5sat,float_inv,float_half,float_i2f,oddparity,bitcount
runners: operator-check,signmask,bitxor,clearbyte,roundup,negativepart,islargerorequal,logicalshift,swapnibblepairs,secondlowestzerobit,rotaterightbits,fractions,overflowcalc,mul5sat,float_inv,float_half,float_i2f,oddparity,bitcount,bitreverse
24 changes: 24 additions & 0 deletions bits.c
Original file line number Diff line number Diff line change
Expand Up @@ -631,3 +631,27 @@ int bitCount(int x) {

return x;
}

// P19
/*
* bitReverse - Reverse bits in an 32-bit integer
* Examples: bitReverse(0x80000004) = 0x20000001
* bitReverse(0x7FFFFFFF) = 0xFFFFFFFE
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 56
* Rating: 2
*/
int bitReverse(int x)
{
int m1 = 255 + (255 << 8);
int m2 = m1 ^ (m1 << 8);
int m3 = m2 ^ (m2 << 4);
int m4 = m3 ^ (m3 << 2);
int m5 = m4 ^ (m4 << 1);
x = ((x >> 16) & m1) + (x << 16);
x = ((x & m2) << 8) + ((x >> 8) & m2);
x = ((x & m3) << 4) + ((x >> 4) & m3);
x = ((x & m4) << 2) + ((x >> 2) & m4);
x = ((x & m5) << 1) + ((x >> 1) & m5);
return x;
}
2 changes: 2 additions & 0 deletions bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ int bitXor(int x, int y);
int negativePart(int x);
int isLargerOrEqual(int x, int y);
int bitCount(int x);
int bitReverse(int x);
int logicalShift(int x, int n);
unsigned float_half(unsigned uf);

Expand All @@ -34,6 +35,7 @@ int test_signMask(void);
int test_negativePart(int x);
int test_isLargerOrEqual(int x, int y);
int test_bitCount(int x);
int test_bitReverse(int x);
int test_logicalShift(int x, int n);
unsigned test_float_half(unsigned uf);
int test_mul5Sat(int x);
Loading
Loading