diff options
Diffstat (limited to 'shift.c')
-rw-r--r-- | shift.c | 32 |
1 files changed, 32 insertions, 0 deletions
@@ -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; +} |