Created
October 23, 2018 10:27
-
-
Save MuhammadJahidHasan/9b9cdb991061571cdeed25f5ef15266d to your computer and use it in GitHub Desktop.
How to check parity in binary number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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