diff options
Diffstat (limited to 'src/c/bit-shift/shift.c')
-rw-r--r-- | src/c/bit-shift/shift.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/c/bit-shift/shift.c b/src/c/bit-shift/shift.c new file mode 100644 index 0000000..c65b815 --- /dev/null +++ b/src/c/bit-shift/shift.c @@ -0,0 +1,32 @@ +#include <stdio.h> + +void print_binary(unsigned char value) { + for (int i = 7; i >= 0; i--) { + printf("%d", (value >> i) & 1); + } + printf("\n"); +} + +int main() { + unsigned char value = 0; // start with all bits off + int bit; + + printf("Starting value: "); + print_binary(value); + + while (1) { + printf("Enter bit to toggle (0-7), or -1 to quit: "); + if (scanf("%d", &bit) != 1) break; + if (bit == -1) break; + if (bit < 0 || bit > 7) { + printf("Invalid bit.\n"); + continue; + } + + value ^= (1 << bit); // toggle the chosen bit + printf("Current value: "); + print_binary(value); + } + + return 0; +} |