Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MuhammadJahidHasan/9b9cdb991061571cdeed25f5ef15266d to your computer and use it in GitHub Desktop.
Save MuhammadJahidHasan/9b9cdb991061571cdeed25f5ef15266d to your computer and use it in GitHub Desktop.
How to check parity in binary number
#include<iostream>
#include <stdio.h>
# define bool int
using namespace std;
bool getParity(unsigned int n)
{
bool parity = 0;
while (n)
{
parity = !parity;
n = n & (n - 1);
}
return parity;
}
int main()
{
unsigned int n;
scanf("%d",&n);
int p=getParity(n);
if(p==0){printf("even\n");}
else
printf("odd\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment