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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| #include <stdio.h> #include <stdint.h>
void printBinary(unsigned char value, int bitlen) { int i = bitlen-1; for (i; i >= 0; i--) { printf("%d", (value >> i) & 0x1); } printf("\n"); }
void increamentValue(unsigned char *value, int group) { int shift = group * 2; unsigned char mask = 0x3 << shift; unsigned char groupValue = (*value & mask) >> shift; if (groupValue < 3) { groupValue++; *value = (*value & ~mask) | (groupValue << shift); } }
int main(int argc, char *argv[]) { unsigned char value = 0x0; printBinary(value, 8);
increamentValue(&value, 0); increamentValue(&value, 0); increamentValue(&value, 0);
increamentValue(&value, 2); increamentValue(&value, 2); increamentValue(&value, 2);
increamentValue(&value, 1); increamentValue(&value, 1); increamentValue(&value, 1);
printBinary(value, 8); return 0; }
|