1 # This code is contributed by
2 # Shubham Singh(SHUBHAMSINGH10)
3 # 2020, modified (@iceman1001)
7 # Python3 program to illustrate Compute the
8 # parity of a number using XOR
9 # Generating the look-up table while pre-processing
11 table
.extend([n
, n ^
1, n ^
1, n
])
13 return (P2(n
, table
), P2(n ^
1, table
),
14 P2(n ^
1, table
), P2(n
, table
))
16 return (P4(n
, table
), P4(n ^
1, table
),
17 P4(n ^
1, table
), P4(n
, table
))
19 return (P6(0, table
), P6(1, table
),
20 P6(1, table
), P6(0, table
))
22 # LOOK_UP is the macro expansion to generate the table
26 # Function to find the parity
28 # Number is considered to be of 32 bits
31 # Dividing the number o 8-bit
32 # chunks while performing X-OR
34 num
= num ^
(num
>> max)
37 # Masking the number with 0xff (11111111)
38 # to produce valid 8-bit result
39 return table
[num
& 0xff]
42 if(len(sys
.argv
) < 2):
44 \t{0} - Calculate parity of a given number
46 Usage: {0} <2,10,16> <number>
48 \t Specify type as in 2 Bin, 10 Decimal, 16 Hex, and number in that particular format
49 \t number can only be 32bit long.
55 Should produce the output:
57 \tOdd parity\n""".format(sys
.argv
[0]))
61 numtype
= int(sys
.argv
[1], 10)
62 print("numtype: {0}".format(numtype
))
63 input= int(sys
.argv
[2], numtype
)
64 print("num: {0} 0x{0:X}".format(input))
66 #num = "001111100010100011101010111101011110"
67 # Result is 1 for odd parity
69 # result = Parity( int(input, numtype) )
70 result
= Parity(input)
71 print("Odd parity") if result
else print("Even parity")
74 if __name__
== "__main__":