9511_workbook

solved exercises from algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
guia07/ex01.c (784B)
   1 #include <stdio.h>
   2 
   3 
   4 typedef enum {
   5     OK,
   6     ERROR
   7 } status_t;
   8 
   9 status_t print_byte(unsigned char *byte);
  10 
  11 int main (void)
  12 {
  13     unsigned char a, b, res;
  14 
  15     a = 0b11001010;
  16     b = 0b10100101;
  17 
  18     printf("a = ");
  19     print_byte(&a);
  20 
  21     printf("b = ");
  22     print_byte(&b);
  23     putchar('\n');
  24         
  25     printf("& = ");
  26     res = (a & b);
  27     print_byte(&res);
  28 
  29     printf("| = ");
  30     res = (a | b);
  31     print_byte(&res);
  32 
  33 
  34     printf("^ = ");
  35     res = (a ^ b);
  36     print_byte(&res);
  37     return OK;
  38 }
  39 
  40 status_t print_byte(unsigned char *byte) 
  41 {
  42     if(byte == NULL) return ERROR;
  43 
  44     unsigned char aux = 0x01;
  45     for(int i = ((sizeof(*byte) * 8) - 1); i >= 0; i--)
  46         putchar(((*byte) & (aux << i)) ? '1' : '0');
  47 
  48     putchar('\n');
  49     return OK;
  50 }
  51 
  52 
  53 
  54 //    for(size_t i = 0; i < (sizeof(*byte) * 8); i++)
  55 //        putchar(((*byte) & (aux << i)) ? '1' : '0');