Parity of a Word:
A binary word’s parity is:
- If the word contains an odd number of ones, it is assigned a value of 1.
- If the word contains an even number of ones, it is assigned a value of 0.
For Example:
Given word = 0111 (7)
No of 1’s = 3
The given word has an odd number of 1’s. Hence the parity is 1.
Examples:
Example1:
Input:
Given Number =7
Output:
 The parity of the given number = 1
Example2:
Input:
Given Number =10
Output:
 The parity of the given number = 0
Program for Computing Parity of a Word in Python
Method #1: Using XOR Operator (Static Input)
Approach:
- Give the number as static input and store it in a variable.
- Take a variable and initialize its value with 0.
- Loop until the given number is not equal to 0 using the while loop.
- Check if the AND operation of the given number and 1 is equal to 1 using the And operator and if conditional statement. (Determine whether the current LSB is 1 or 0.)
- If it is true, then perform the XOR operation for the above-initalized variable and 1 and store it in the same variable. It will alternately change the parity from 0 to 1 and vice versa.
- Right shift the given number and store it in the same variable.
- Print the parity of the given number.
- The Exit of the Program.
Below is the implementation:
# Give the number as static input and store it in a variable. gvn_num = 7 # Take a variable and initialize its value with 0. rslt_parityy = 0 # Loop until the given number is not equal to 0 using the while loop. while(gvn_num != 0): # Check if the AND operation of the given number and 1 is equal to 1 using the # And operator and if conditional statement. # (Determine whether the current LSB is 1 or 0.) if((gvn_num & 1) == 1): # If it is true, then perform the XOR operation for the above-initalized # variable and 1 and store it in the same variable. # It will alternately change the parity from 0 to 1 and vice versa. rslt_parityy ^= 1 # Right shift the given number and store it in the same variable. gvn_num >>= 1 # Print the parity of the given number. print("The parity of the given number = ", rslt_parityy)
Output:
The parity of the given number = 1
Method #2: Using XOR Operator (User Input)
Approach:
- Give the number as user input using the int(input()) function and store it in a variable.
- Take a variable and initialize its value with 0.
- Loop until the given number is not equal to 0 using the while loop.
- Check if the AND operation of the given number and 1 is equal to 1 using the And operator and if conditional statement. (Determine whether the current LSB is 1 or 0.)
- If it is true, then perform the XOR operation for the above-initalized variable and 1 and store it in the same variable. It will alternately change the parity from 0 to 1 and vice versa.
- Right shift the given number and store it in the same variable.
- Print the parity of the given number.
- The Exit of the Program.
Below is the implementation:
# Give the number as user input and store it in a variable. gvn_num = int(input('Enter some random number = ')) # Take a variable and initialize its value with 0. rslt_parityy = 0 # Loop until the given number is not equal to 0 using the while loop. while(gvn_num != 0): # Check if the AND operation of the given number and 1 is equal to 1 using the # And operator and if conditional statement. # (Determine whether the current LSB is 1 or 0.) if((gvn_num & 1) == 1): # If it is true, then perform the XOR operation for the above-initalized # variable and 1 and store it in the same variable. # It will alternately change the parity from 0 to 1 and vice versa. rslt_parityy ^= 1 # Right shift the given number and store it in the same variable. gvn_num >>= 1 # Print the parity of the given number. print("The parity of the given number = ", rslt_parityy)
Output:
Enter some random number = 10 The parity of the given number = 0