bits.h 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #ifndef __BITS
  2. #define __BITS
  3. #include <stdint.h>
  4. #include <stdbool.h>
  5. #include <stdlib.h>
  6. typedef bool bit;
  7. typedef uint_fast16_t bit_array;
  8. typedef char byte;
  9. /* ceil(a / b) == ((a - 1) / b) + 1
  10. * I knew a truly marvellous proof of this, which
  11. * this 80x24 comment is too narrow to contain.
  12. */
  13. #define devide_ceil(a, b) (((a - 1) / b) + 1)
  14. #define SIZE_IN_BITS(i) (sizeof(i) * 8)
  15. #define SIZE(i) (devide_ceil((i), SIZE_IN_BITS(bit_array)))
  16. #define BIT_SET(a,x) ((a) |= ((bit_array) 1 << (x)))
  17. #define BIT_CLEAR(a,x) ((a) &= ~((bit_array) 1 << (x)))
  18. #define BIT_CHECK(a,x) (((a) & ((bit_array) 1 << (x))) != 0)
  19. bit get_bit(bit_array *bits, size_t index);
  20. void set_bit(bit_array *bits, size_t index);
  21. void clear_bit(bit_array *bits, size_t index);
  22. void hex2bits(bit_array *bits, size_t index, uint16_t hex);
  23. uint16_t bits2hex(bit_array *bits, size_t index);
  24. void char2bits(bit_array *bits, size_t index, char chr);
  25. char bits2char(bit_array *bits, size_t index);
  26. void bits_left_shift(bit_array *bits, int len, int offset);
  27. #endif