Advertisement
STANAANDREY

bits parity

Oct 24th, 2022 (edited)
866
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4. int getBitsParity(uint32_t x) {
  5.   int parity = 0;
  6.   if (x) {
  7.     do {
  8.       parity ^= 1;
  9.     } while(x &= x - 1);
  10.   }
  11.   return parity;
  12. }
  13.  
  14. int main(void) {
  15.   uint32_t x;
  16.   printf("x="); scanf("%u", &x);
  17.   printf("bits parity: %d\n", getBitsParity(x));
  18.   return 0;
  19. }
  20.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement