- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this article, we will be discussing a program to find the parity of a given number N.

Parity is defined as the number of set bits (number of ‘1’s) in the binary representation of a number.

If the number of ‘1’s in the binary representation are even, the parity is called even parity and if the number of ‘1’s in the binary representation is odd, the parity is called odd parity.

If the given number is N, we can perform the following operations.

- y = N ^ (N >> 1)
- y = y ^ (y >> 2)
- y = y ^ (y >> 4)
- y = y ^ (y >> 8)
- y = y ^ (y >> 16)

Once all these operations are done, the rightmost bit in y will represent the parity of the number. If the bit is 1, the parity would be odd and if the bit would be 0, the parity will be even.

#include <bits/stdc++.h> using namespace std; bool calc_parity(int N) { int y; y= N ^ (N >> 1); y = y ^ (y >> 2); y = y ^ (y >> 4); y = y ^ (y >> 8); y = y ^ (y >> 16); //checking the rightmost bit if (y & 1) return 1; return 0; } int main() { int n=1345; int result = calc_parity(n); if(result==1) cout << "Odd Parity" << endl; else cout << "Even Parity" << endl; return 0; }

Even Parity

- Related Questions & Answers
- Finding the Parity of a number Efficiently in C++
- Golang Program to find the parity of a given number.
- Program to invert bits of a number Efficiently in C++
- Program to find parity in C++
- Parity Check of a Number
- C Program for efficiently print all prime factors of a given number?
- C/C++ Program to Find the Number Occurring Odd Number of Times?
- C++ Program to Find the Number of Permutations of a Given String
- C Program to Find the minimum sum of factors of a number?
- C/C++ Program to find the Product of unique prime factors of a number?
- C# program to find the sum of digits of a number using Recursion
- 8085 program to find the factorial of a number
- 8086 program to find the factorial of a number
- C++ Program to find whether a number is the power of two?
- C++ Program to Find the Number of occurrences of a given Number using Binary Search approach

Advertisements