7
The computational parity of an integer is defined as 1 if the integer has an odd number of set bits, and 0 otherwise. (src).
Input
- You will receive one nonnegative integer that you will take the parity of. Optionally, you may take the size of the integer in bytes or bits as an argument, but you must specify this in your answer.
Output
- You will output the computational parity of the input integer.
Rules
- Standard loopholes apply, including but not limited to Abusing native number types to trivialize a problem.
Test cases
You may omit any input that cannot be represented with your language's built-in integer type.
0: 0
1: 1
3: 0
8: 1
13: 1
17: 0
40: 0
52: 1
100: 1
127: 1
365: 0
787: 1
2898: 0
6345: 0
9038: 1
10921: 1
16067: 1
4105748: 1
40766838: 0
1336441507: 1
4294967295: 0
Generated using this program.
The answer with the least amount of bytes in each language wins, which means that I will not be accepting an answer.
Bonus imaginary Internet points for an explanation.
2I don't understand why this is closed... computational parity and regular parity aren't the same thing. The question linked as the dupe is for regular parity. – RGS – 2020-02-11T18:35:20.163
1
I don't think Shaggy has the right dupe target. If anything, this is closer to a dupe.
– AdmBorkBork – 2020-02-11T18:35:46.2401@AdmBorkBork - Yes, yours is closer, but even that isn't a dupe. – Jeff Zeitlin – 2020-02-11T18:37:57.673
2Unfortunately, I don't have enough rep in this stack to VTR - I have raised a flag to the moderators asking that it be reopened as not-a-dupe, however. – Jeff Zeitlin – 2020-02-11T19:03:02.910
Please check your test cases. It appears that your last test case, 429496295, is wrong. – Jeff Zeitlin – 2020-02-11T19:11:22.277
2How do you define "counting the number of set bits"? – Jeff Zeitlin – 2020-02-11T19:15:44.090
2I reopened this because it is not a duplicate but I have voted to close this since "You may not calculate the Hamming weight of the integer. " is not clear. – Post Rock Garf Hunter – 2020-02-11T21:03:32.860
2In general "you must not calculate X as an intermediate step" sort of rules are really problematic since there is no way to objectively decide whether a program does or doesn't. This one is especially problematic because often the desired output is the hamming weight. For example any power of two. – Post Rock Garf Hunter – 2020-02-11T21:05:37.040
2
I was pretty sure I already answered this before. I think it's actually a dupe of this challenge. The only difference is that it's expecting the opposite output.
– Arnauld – 2020-02-11T21:47:08.690@Arnauld Expecting the opposite output does make a substantial difference in some of the answers, though. – S.S. Anne – 2020-02-11T22:02:16.890
5Inverting the output is not really enough to justify a new challenge. – Jo King – 2020-02-11T23:43:21.627