1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef unsigned char uchar;
uchar get_bit(uchar ch, int bit_idx) {
ch = ch << bit_idx;
return ch >> 7;
//return (uchar)(ch << bit_idx) >> 7; // also okay
//return (ch << bit_idx) >> 7; // wrong!!
//return (ch << (uchar)bit_idx) >> 7; // wrong!! ... but why?
}
int main(int argc, char* argv[]) {
assert( get_bit(0b11001001, 0) == 1 );
assert( get_bit(0b11001001, 2) == 0 );
return EXIT_SUCCESS;
}
/* printf("get_bit(0b11001001, 0) == %d\n", (int)get_bit(0b11001001, 0)); */
/* printf("get_bit(0b11001001, 2) == %d\n", (int)get_bit(0b11001001, 2)); */
/* 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.