Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. void display_flags(char *, unsigned int);
  4. void binary_print(unsigned int);
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8. display_flags("O_RDONLY\t\t", O_RDONLY);
  9. display_flags("O_WRONLY\t\t", O_WRONLY);
  10. display_flags("O_RDWR\t\t\t", O_RDWR);
  11. printf("\n");
  12. display_flags("O_APPEND\t\t", O_APPEND);
  13. display_flags("O_TRUNC\t\t\t", O_TRUNC);
  14. display_flags("O_CREAT\t\t\t", O_CREAT);
  15. printf("\n");
  16. display_flags("O_WRONLY|O_APPEND|O_CREAT", O_WRONLY|O_APPEND|O_CREAT);
  17. }
  18. void display_flags(char *label, unsigned int value)
  19. {
  20. printf("%s\t: %d\t:", label, value);
  21. binary_print(value);
  22. printf("\n");
  23. }
  24. void binary_print(unsigned int value)
  25. {
  26. unsigned int mask = 0xff000000; // Start with a mask for the highest byte.
  27. unsigned int shift = 256*256*256; // Start with a shift for the highest byte.
  28. unsigned int byte, byte_iterator, bit_iterator;
  29. for(byte_iterator=0; byte_iterator < 4; byte_iterator++)
  30. {
  31. byte = (value & mask) / shift; // Isolate each byte.
  32. printf(" ");
  33. for(bit_iterator=0; bit_iterator < 8; bit_iterator++) // Print the byte's bits.
  34. {
  35. if(byte & 0x80) // If the highest bit in the byte isn't 0,
  36. printf("1"); // print a 1.
  37. else
  38. printf("0"); // Otherwise, print a 0.
  39. byte *= 2; // Move all the bits to the left by 1.
  40. }
  41. mask /= 256; // Move the bits in mask right by 8.
  42. shift /= 256; // Move the bits in shift right by 8.
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement