User Tools

Site Tools


finding_20the_20parity_20of_20a_20number

Finding the parity of a number

by Richard Russell, March 2014

The parity of an integer is determined by the number of set (i.e. 1) bits in its binary representation. If there are an even number of set bits the parity is even and if there are an odd number of set bits the parity is odd. So for example %00100000 has odd parity and %00100010 has even parity.

The function below can be used to discover the parity of a 32-bit integer, it returns 0 for even parity and 1 for odd parity:

        DEF FNparity(X%)
        X% EOR= X% >> 1
        X% EOR= X% >> 2
        X% EOR= X% >> 4
        X% EOR= X% >> 8
        X% EOR= X% >> 16
        = X% AND 1 

An equivalent function for 64-bit integers is as follows:

        DEF FNparity(X%%)
        X%% EOR= X%% >> 1
        X%% EOR= X%% >> 2
        X%% EOR= X%% >> 4
        X%% EOR= X%% >> 8
        X%% EOR= X%% >> 16
        X%% EOR= X%% >> 32
        = X%% AND 1 
This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
finding_20the_20parity_20of_20a_20number.txt · Last modified: 2024/01/05 00:22 by 127.0.0.1