Advertisement
STANAANDREY

consecutive bits

Jan 23rd, 2023 (edited)
917
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4. void printBin(uint64_t x) {
  5.   for (int i = sizeof(x) * 8 - 1; i >= 0; i--) {
  6.     printf("%d", (int)(1 & (x >> i)));
  7.   }
  8.   puts("");
  9. }
  10.  
  11. int one(uint64_t x, int pos) {
  12.   return 1 & (x >> pos);
  13. }
  14.  
  15. int main(void) {
  16.   uint64_t x;
  17.   scanf("%lu", &x);
  18.   printBin(x);
  19.   int len = 0;
  20.   for (int i = 0; i < sizeof(x) * 8; i++) {
  21.     if (one(x, i)) {
  22.       len++;
  23.     } else {
  24.       if (i && one(x, i - 1)) {
  25.         printf("%d ", len);
  26.       }
  27.       len = 0;
  28.     }
  29.   }
  30.   putchar('\n');
  31.   return 0;
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement