1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <stdio.h>
#include <stdlib.h>
#include <assert.h>
// THIS SOLUTION IS NOT YET COMPLETE
//
// See bitwise_finished.c
unsigned char get_bits(unsigned char* buf, int start_bit, int end_bit) {
unsigned char ch = buf[0];
unsigned char bits = ch << start_bit; // knock off 'start_bit' bits
int num_bits = end_bit - start_bit + 1;
bits = bits >> (8 - num_bits);
return bits;
}
int main(int argc, char* argv[]) {
unsigned char buf[256] = {255, 127, 63, 31}; // initializes to zero
assert( get_bits(buf, 2, 6) == 32 - 1 );
printf("%d\n", get_bits(buf, 2, 6));
return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2017 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.