Guest User

C

a guest
Feb 20th, 2015
561
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. int main()
  6. {
  7. unsigned int n = 20;
  8. int* source = (int*)malloc(n * sizeof(int));
  9.  
  10. srand(1);
  11. for (unsigned int i = 0; i < n; ++i)
  12. {
  13. source[i] = rand() % 2;
  14. }
  15.  
  16. unsigned int k = (n + 7) / 8;
  17. uint8_t* compressed = (uint8_t*)calloc(k, sizeof(uint8_t));
  18.  
  19. unsigned int i_n = 0;
  20. for (unsigned int i = 0; i < k; ++i)
  21. {
  22. for (int j = 7; j >= 0 && i_n < n; --j, ++i_n)
  23. {
  24. if (source[i_n])
  25. {
  26. compressed[i] |= source[i_n] << j;
  27. }
  28. }
  29. }
  30.  
  31. for (unsigned int i = 0; i < n; ++i)
  32. {
  33. printf("%d", source[i]);
  34. }
  35.  
  36. printf("\n");
  37.  
  38. for (unsigned int i = 0; i < k; ++i)
  39. {
  40. for (int j = 7; j >= 0; --j)
  41. {
  42. printf("%d", (compressed[i] & (1 << j)) ? 1 : 0);
  43. }
  44. }
  45.  
  46. free(compressed);
  47. free(source);
  48. }
Advertisement
Add Comment
Please, Sign In to add comment