9511_workbook

solved exercises from algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
commit bb0227647e34180a62c2d3c29f4fefa44ce706f9
parent 95098aaaa58dc7ec8d78809459dfad11d2924ca4
Author: klewer-martin <martin.cachari@gmail.com>
Date:   Sat, 13 Feb 2021 15:57:09 -0300

Update: Added ex04.c;

Diffstat:
Aguia07/ex04.c | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 58 insertions(+), 0 deletions(-)
diff --git a/guia07/ex04.c b/guia07/ex04.c
@@ -0,0 +1,58 @@
+#include <stdio.h>
+
+#define BIT_MASK 0b00000001
+
+#define MASK_STATUS 0b10000000
+
+unsigned char reg;
+
+void print_byte(unsigned char *reg);
+void turnOn(unsigned char bit);
+void turnOff(unsigned char bit);
+
+int main (void)
+{
+    reg = 0b01010101;
+    
+    reg = reg & ~(BIT_MASK << 7);
+    print_byte(&reg);
+
+    putchar('\n');
+    reg = reg | (BIT_MASK << 7);
+    print_byte(&reg);
+
+    putchar('\n');
+    reg = reg & ~(BIT_MASK << 7);
+    print_byte(&reg);
+
+    turnOn(MASK_STATUS);
+    putchar('\n');
+    print_byte(&reg);
+
+    turnOff(MASK_STATUS);
+    putchar('\n');
+    print_byte(&reg);
+    return 0;
+}
+
+
+
+void print_byte(unsigned char *reg)
+{
+    int i;
+    for(i = (sizeof(*reg) * 8); i > 0; i--)
+        putchar((BIT_MASK << (i - 1)) & (*reg) ? '1' : '0');
+
+    return;
+}
+
+void turnOn(unsigned char bit) 
+{
+    reg = reg | bit;
+}
+
+
+void turnOff(unsigned char bit) 
+{
+    reg = reg & ~(bit);
+}