Parity Checker

16 Jan 2021 - Syed Muhammad Shahrukh Hussain

Write a function to find even or odd parity of a number.

Brief

This program will create a function int parity(int num) and return 1 in case of even parity and 0 for odd parity.

C

#include <stdio.h>

int parity_check(int number) {
    
    int parity = 1;
    
    while (number != 0 ){
     
        parity ^= (number & 1);
        
        number = number >> 1;
    }    
    
    return parity;
}

int main () {
    
    printf("even parity: %d\n", parity_check(3));
    printf("odd parity: %d\n", parity_check(8));
}

Python

#!/usr/bin/python3
def parity_check(number):
    
    parity = 1;
    while (number != 0 ):
        parity ^= (number & 1);
        number = number >> 1;
    return parity;

def __main__ ():
    print("even parity:", parity_check(3));
    print("odd parity:", parity_check(8));


__main__() # call main function