fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. static unsigned char* read_bits(const unsigned char *src, int offset, int nbits){
  5. int bit, byte, bit_position;
  6. unsigned char *s = calloc(1, nbits + 1);
  7. if(s){
  8. --nbits;
  9. for(bit = offset; nbits >= 0; ++bit, --nbits){
  10. byte = bit / 8;
  11. bit_position = bit % 8;
  12. s[nbits] = (src[byte] >> bit_position) & 1;
  13. }
  14. }
  15. return s;
  16. }
  17.  
  18. int main(void) {
  19. unsigned char *x = "oo";
  20. printf("%s -> %s\n", x, read_bits(x, 0, 16)); //should print "oo -> 0110111101101111"
  21. return 0;
  22. }
  23.  
  24.  
  25.  
Success #stdin #stdout 0s 2380KB
stdin
Standard input is empty
stdout
oo ->