{"id":25741,"date":"2021-12-04T09:19:52","date_gmt":"2021-12-04T03:49:52","guid":{"rendered":"https:\/\/python-programs.com\/?p=25741"},"modified":"2021-12-04T09:19:52","modified_gmt":"2021-12-04T03:49:52","slug":"python-program-for-computing-parity-of-a-word","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-computing-parity-of-a-word\/","title":{"rendered":"Python Program for Computing Parity of a Word."},"content":{"rendered":"
Parity of a Word:<\/strong><\/p>\n A binary word’s parity is:<\/p>\n For Example:<\/p>\n Given word = 0111 (7)<\/p>\n No of 1’s = 3<\/p>\n The given word has an odd number of 1’s. Hence the parity is 1.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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 …<\/p>\n\n
Given Number =7<\/pre>\n
\u00a0The parity of the given number = 1<\/pre>\n
Given Number =10<\/pre>\n
\u00a0The parity of the given number = 0<\/pre>\n
Program for Computing Parity of a Word in Python<\/h2>\n
\n
Method #1: Using XOR Operator (Static Input)<\/h3>\n
\n
# Give the number as static input and store it in a variable.\r\ngvn_num = 7\r\n# Take a variable and initialize its value with 0.\r\nrslt_parityy = 0\r\n# Loop until the given number is not equal to 0 using the while loop.\r\nwhile(gvn_num != 0):\r\n\r\n # Check if the AND operation of the given number and 1 is equal to 1 using the\r\n # And operator and if conditional statement.\r\n # (Determine whether the current LSB is 1 or 0.)\r\n if((gvn_num & 1) == 1):\r\n # If it is true, then perform the XOR operation for the above-initalized\r\n # variable and 1 and store it in the same variable.\r\n # It will alternately change the parity from 0 to 1 and vice versa.\r\n rslt_parityy ^= 1\r\n\r\n # Right shift the given number and store it in the same variable.\r\n gvn_num >>= 1\r\n# Print the parity of the given number.\r\nprint(\"The parity of the given number = \", rslt_parityy)<\/pre>\n
The parity of the given number = 1<\/pre>\n
Method #2: Using XOR Operator (User Input)<\/h3>\n
\n
# Give the number as user input and store it in a variable.\r\ngvn_num = int(input('Enter some random number = '))\r\n# Take a variable and initialize its value with 0.\r\nrslt_parityy = 0\r\n# Loop until the given number is not equal to 0 using the while loop.\r\nwhile(gvn_num != 0):\r\n\r\n # Check if the AND operation of the given number and 1 is equal to 1 using the\r\n # And operator and if conditional statement.\r\n # (Determine whether the current LSB is 1 or 0.)\r\n if((gvn_num & 1) == 1):\r\n # If it is true, then perform the XOR operation for the above-initalized\r\n # variable and 1 and store it in the same variable.\r\n # It will alternately change the parity from 0 to 1 and vice versa.\r\n rslt_parityy ^= 1\r\n\r\n # Right shift the given number and store it in the same variable.\r\n gvn_num >>= 1\r\n# Print the parity of the given number.\r\nprint(\"The parity of the given number = \", rslt_parityy)<\/pre>\n
Enter some random number = 10 \r\nThe parity of the given number = 0<\/pre>\n","protected":false},"excerpt":{"rendered":"