fork download
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stddef.h>
  4. #include <stdbool.h>
  5. #include <limits.h>
  6.  
  7. uint8_t ee_fat[10] = {0,};
  8.  
  9. bool getBit (uint8_t* p, size_t index) {
  10. const size_t entrySize = sizeof(p[0]) * CHAR_BIT;
  11. return p[index / entrySize] & (1 << (index % entrySize));
  12. }
  13. void setBit (uint8_t* p, size_t index, bool value) {
  14. const size_t entrySize = sizeof(p[0]) * CHAR_BIT;
  15. const size_t bit = index % entrySize;
  16. p[index / entrySize] = (p[index / entrySize] & ~(1 << bit)) | (value << bit);
  17. }
  18.  
  19. void print (uint8_t* p, size_t length) {
  20. for (size_t i = 0; i < length; ++i) {
  21. for (size_t j = 0; j < CHAR_BIT * sizeof(p[0]); ++j) {
  22. printf ("%d, ", (p[i] >> j) & 1);
  23. }
  24. }
  25. printf ("\n");
  26. }
  27.  
  28. int main () {
  29. setBit (ee_fat, 7, 1);
  30. setBit (ee_fat, 42, 1);
  31. print (ee_fat, sizeof(ee_fat)/sizeof(ee_fat[0]));
  32. printf ("%d\n", getBit (ee_fat, 7));
  33. printf ("%d\n", getBit (ee_fat, 42));
  34. }
Success #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
1
1