I am just starting to get my feet wet with C; and am rather enjoying myself in doing so. This is sort of a follow up to my last question here. I am posting this ordinate to hopefully getting some feedback on the logic itself, and possibly also some feedback on coding style. I do, however insist on making everything explicit. I'm not a huge fan of implicit anything... Maybe that's something I need to get over?
#include <stdio.h>
typedef enum {
false = (int) 0, true = (int) 1
} bool;
inline void set_bit(register unsigned int *x,
register const unsigned short int offset, register const bool value) {
if (value)
*x |= (1 << offset);
else
*x &= ~(1 << offset);
}
inline bool get_bit(register const unsigned int x,
register const unsigned short int offset) {
return (x & (1 << offset)) != 0;
}
int main(void) {
unsigned int x = 0;
int count;
for (count = 0; count < 8; count += 2)
set_bit(&x, count, true);
for (count = 7; count >= 0; count--)
printf("%d ", (int) get_bit(x, count));
printf("\n");
return 0;
}
From most of the suggestions of answers I have on my screen, I have reduced the two functions down to:
unsigned set_bit(const unsigned x, const unsigned offset, const bool value) {
return value ? x | (1 << offset) : x & ~(1 << offset);
}
bool get_bit(const unsigned x, const unsigned offset) {
return (x & (1 << offset)) != 0;
}
They look so trivial now.
bool
datatype.#include <stdbool.h>
– user2357112 yesterday