Parity: Parity of a number refers to whether it contains an odd or even number of 1-bits. The number has “odd parity” if it contains an odd number of 1-bits and is “even parity” if it contains an even number of 1-bits.
The main idea of the below solution is – Loop while n is not 0 and in loop unset one of the set bits and invert parity.
Algorithm: getParity(n)
1. Initialize parity = 0
2. Loop while n != 0
a. Invert parity
parity = !parity
b. Unset rightmost set bit
n = n & (n-1)
3. return parity
Example:
Initialize: n = 13 (1101) parity = 0
n = 13 & 12 = 12 (1100) parity = 1
n = 12 & 11 = 8 (1000) parity = 0
n = 8 & 7 = 0 (0000) parity = 1Program:
C++
# include<bits/stdc++.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 = 7;
cout<<"Parity of no "<<n<<" = "<<(getParity(n)? "odd": "even");
getchar();
return 0;
}
|
C
# include <stdio.h>
# define bool int
bool getParity(unsigned int n)
{
bool parity = 0;
while (n)
{
parity = !parity;
n = n & (n - 1);
}
return parity;
}
int main()
{
unsigned int n = 7;
printf("Parity of no %d = %s", n,
(getParity(n)? "odd": "even"));
getchar();
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigInteger;
class GFG
{
static boolean getParity(int n)
{
boolean parity = false;
while(n != 0)
{
parity = !parity;
n = n & (n-1);
}
return parity;
}
public static void main (String[] args)
{
int n = 7;
System.out.println("Parity of no " + n + " = " +
(getParity(n)? "odd": "even"));
}
}
|
Python3
def getParity( n ):
parity = 0
while n:
parity = ~parity
n = n & (n - 1)
return parity
n = 7
print ("Parity of no ", n," = ",
( "odd" if getParity(n) else "even"))
|
C#
using System;
class GFG {
static bool getParity(int n)
{
bool parity = false;
while(n != 0)
{
parity = !parity;
n = n & (n-1);
}
return parity;
}
public static void Main ()
{
int n = 7;
Console.Write("Parity of no " + n
+ " = " + (getParity(n)?
"odd": "even"));
}
}
|
PHP
<?php
function getParity( $n)
{
$parity = 0;
while ($n)
{
$parity = !$parity;
$n = $n & ($n - 1);
}
return $parity;
}
$n = 7;
echo "Parity of no ",$n ," = " ,
getParity($n)? "odd": "even";
?>
|
Javascript
<script>
function getParity(n)
{
var parity = false;
while(n != 0)
{
parity = !parity;
n = n & (n - 1);
}
return parity;
}
var n = 7;
document.write("Parity of no " + n + " = " +
(getParity(n) ? "odd": "even"));
</script>
|
OutputParity of no 7 = odd
Above solution can be optimized by using lookup table. Please refer to Bit Twiddle Hacks[1st reference] for details.
Time Complexity: The time taken by above algorithm is proportional to the number of bits set. Worst case complexity is O(Log n).
Auxiliary Space: O(1)
Another approach: (Using built-in-function)
C++
# include<bits/stdc++.h>
# define bool int
using namespace std;
bool getParity(unsigned int n)
{
return __builtin_parity(n);
}
int main()
{
unsigned int n = 7;
cout<<"Parity of no "<<n<<" = "<<(getParity(n)? "odd": "even");
getchar();
return 0;
}
|
OutputParity of no 7 = odd
Uses: Parity is used in error detection and cryptography.
Compute the parity of a number using XOR and table look-up
References:
http://graphics.stanford.edu/~seander/bithacks.html#ParityNaive – last checked on 30 May 2009.