9511_workbook

solved exercises from algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
guia07/ex04.c (828B)
   1 #include <stdio.h>
   2 
   3 #define BIT_MASK 0b00000001
   4 
   5 #define MASK_STATUS 0b10000000
   6 
   7 unsigned char reg;
   8 
   9 void print_byte(unsigned char *reg);
  10 void turnOn(unsigned char bit);
  11 void turnOff(unsigned char bit);
  12 
  13 int main (void)
  14 {
  15     reg = 0b01010101;
  16     
  17     reg = reg & ~(BIT_MASK << 7);
  18     print_byte(&reg);
  19 
  20     putchar('\n');
  21     reg = reg | (BIT_MASK << 7);
  22     print_byte(&reg);
  23 
  24     putchar('\n');
  25     reg = reg & ~(BIT_MASK << 7);
  26     print_byte(&reg);
  27 
  28     turnOn(MASK_STATUS);
  29     putchar('\n');
  30     print_byte(&reg);
  31 
  32     turnOff(MASK_STATUS);
  33     putchar('\n');
  34     print_byte(&reg);
  35     return 0;
  36 }
  37 
  38 
  39 
  40 void print_byte(unsigned char *reg)
  41 {
  42     int i;
  43     for(i = (sizeof(*reg) * 8); i > 0; i--)
  44         putchar((BIT_MASK << (i - 1)) & (*reg) ? '1' : '0');
  45 
  46     return;
  47 }
  48 
  49 void turnOn(unsigned char bit) 
  50 {
  51     reg = reg | bit;
  52 }
  53 
  54 
  55 void turnOff(unsigned char bit) 
  56 {
  57     reg = reg & ~(bit);
  58 }