summaryrefslogtreecommitdiff
path: root/shift.c
blob: c65b815a05dc8251f62f6027e6b78dffb7b0d4ac (plain)
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
#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;
}