HristoBaychev

10CountBitInNumber

Oct 5th, 2021 (edited)
102
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.  
  3.  
  4. void count1s0s(int N)
  5. {
  6.    
  7.     int count0 = 0, count1 = 0;
  8.  
  9.     while (N > 0) {
  10.  
  11.         // ako tek bit e 1
  12.         if (N & 1) {
  13.             count1++;
  14.         }
  15.  
  16.         // ako tek bit e 0
  17.         else {
  18.             count0++;
  19.         }
  20.  
  21.         N = N >> 1;
  22.     }
  23.  
  24.    
  25.     printf("Count of 0s in N is %d\n", count0);
  26.     printf("Count of 1s in N is %d\n", count1);
  27. }
  28.  
  29.  
  30. int main()
  31. {
  32.     // zadadeno chislo
  33.     int N = 10;
  34.  
  35.     // izvikva funkciata
  36.     count1s0s(N);
  37.     return 0;
  38. }
Add Comment
Please, Sign In to add comment