summaryrefslogtreecommitdiff
path: root/src/c/bit-shift
diff options
context:
space:
mode:
Diffstat (limited to 'src/c/bit-shift')
-rwxr-xr-xsrc/c/bit-shift/shiftbin0 -> 15672 bytes
-rw-r--r--src/c/bit-shift/shift.c32
2 files changed, 32 insertions, 0 deletions
diff --git a/src/c/bit-shift/shift b/src/c/bit-shift/shift
new file mode 100755
index 0000000..6429b03
--- /dev/null
+++ b/src/c/bit-shift/shift
Binary files differ
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;
+}