Question: #include #include /* * Project 1: Test skills with bitwise operators. * * Given an array of 8-bit 3-tuples as input, print out various outputs.
#include #include
/*
* Project 1: Test skills with bitwise operators. * * Given an array of 8-bit 3-tuples as input, print out various outputs. * To accomplish, you need to implement the following functions: * * 1. reverse() * 2. clearbits() * 3. shift() * 4. binaryPrint() * 5. getTestCount() * * The description of what each function eneds to do is provided in the * comments above the function stub. * * The main() function should be left unchanged. If all works as expected, * the result of the program should match what is in reference.txt. */
struct ntuple { uint8_t core; uint8_t mask; int8_t shift; };
const struct ntuple inputs[] = { { 0x01, 0x0f, 3 }, { 0x42, 0xf0, -2 }, { 0x03, 0xaa, 0 }, { 0xff, 0xf0, -7 }, { 0xe3, 0xbc, 7 }, { 0x7d, 0x00, -2 }, { 0x88, 0x00, 8 }, { 0xaa, 0x00, 2 }, { 0x01, 0xaa, 3 }, { 0x01, 0xcc, 3 }, { 0xbb, 0xff, 2 }, { 0xcc, 0x67, 4 }, { 0x00, 0x32, 2 } };
/* Take a value x and reverse the order of the bits. */ uint8_t reverse(uint8_t x) { int NO_OF_BITS = sizeof(x) * 8; int reverse_num = 0, i, temp;
for (i = 0; i < NO_OF_BITS; i++) { temp = (x & (1 << i)); if(temp) reverse_num |= (1 << ((NO_OF_BITS - 1) - i)); }
return reverse_num;
}
/* For all of the one bits in mv, clear the corresponding bits in val and * return the result. */ uint8_t clearbits(uint8_t val, uint8_t mv) { }
/* Shift val the number of bits provided by sv. If sv is positive, shift left. * If sv is negative, logically shift right the absolute value. */ uint8_t shift(uint8_t val, int8_t sv) { }
/* Print out the value val in string form to standard output. Assume big * endian. */ void binaryPrint(uint8_t val) { }
/* Calculate and return the number of test values in the input array. * Assume that additional values could be added or removed, so don't just * return a constant 13! */ int getTestCount() { }
int main(int argc, char **argv) { int n = getTestCount();
printf("size is %d ", n);
for (int i=0; i < n; i++) { printf("%02x: ", inputs[i].core); binaryPrint(inputs[i].core); printf(" "); binaryPrint(reverse(inputs[i].core)); printf(" "); binaryPrint(clearbits(inputs[i].core, inputs[i].mask)); printf(" "); binaryPrint(shift(inputs[i].core, inputs[i].shift)); printf(" "); } return 0; }
reference.txt.
size is 13
01: 00000001 10000000 00000000 00001000 42: 01000010 01000010 00000010 00010000 03: 00000011 11000000 00000001 00000011 ff: 11111111 11111111 00001111 00000001 e3: 11100011 11000111 01000011 10000000 7d: 01111101 10111110 01111101 00011111 88: 10001000 00010001 10001000 00000000 aa: 10101010 01010101 10101010 10101000 01: 00000001 10000000 00000001 00001000 01: 00000001 10000000 00000001 00001000 bb: 10111011 11011101 00000000 11101100 cc: 11001100 00110011 10001000 11000000 00: 00000000 00000000 00000000 00000000
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
