Computational Parity Party! [duplicate]

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.

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.

• I 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 Feb 11 at 18:35
• Unfortunately, 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 Feb 11 at 19:03
• How do you define "counting the number of set bits"? – Jeff Zeitlin Feb 11 at 19:15
• I 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 Feb 11 at 21:03
• Inverting the output is not really enough to justify a new challenge. – Jo King Feb 11 at 23:43

JavaScript (ES6),  18  17 bytes

Similar to my answer to Is this number evil? except that the last iteration returns $$\0\$$, which saves a byte.

f=n=>n&&!f(n&~-n)

Try it online!

JavaScript (ES6), 30 bytes

Converts to binary, parses as base-3 and returns the parity.

n=>parseInt(n.toString(2),3)&1

Try it online!

• I think you moved your code up above the lang mark, so it's not getting syntax highlighting. – S.S. Anne Feb 11 at 22:03
• @S.S.Anne Seems like there's nothing to highlight in there, but fixed anyway. :-) – Arnauld Feb 11 at 22:07
• Hmm... That base 3 solution is interesting. Any chance that it could be implemented without converting to a string? – S.S. Anne Feb 11 at 23:11

Jelly, 3 bytes

BSḂ

Try it online! Or see the test-suite.

How?

B   - to binary
S  - sum
Ḃ - least-significant-bit
• Interesting. +1 for the explanation. – S.S. Anne Feb 11 at 21:31
• B^/ is another 3 – Nick Kennedy Feb 11 at 22:08
• On mobile it looks like BS[]. Stupid font encodings... – S.S. Anne Feb 11 at 23:29

Python, 27 bytes

f=lambda n:n and 1-f(n&~-n)

Try it online!

This is quite nice because if we just want to identify the parity we can reduce it to 25 bytes returning 0 or -1:

f=lambda n:n and~f(n&~-n)

Python 2, 27 bytes

f=lambda n:n and n&1^f(n/2)

Try it online!

Fortran (GFortran), 28 bytes

print*,poppar(i)
end

Try it online!

Fortran has this as an in-built since F2008

05AB1E, 5 4 bytes

bSOÉ

Try it online!

Explanation

bSOÉ

b    # convert (implicit) input to base 2
O  # the sum of...
S   # the digits...
É # is odd?

Oasis, 4 bytes

ES2%

Try it online!

Explanation

ES2%

S    # the sum of...
E     # the binary digits...
2%  # mod 2

Ruby, 19 bytes

->n{("%b"%n).sum%2}

Try it online!

Python 3, 28 bytes

lambda n:bin(n).count('1')%2

Try it online!

JavaScript, 18 bytes

Port of ovs' Python solution - be sure to +1 that if you're +1ing this.

f=n=>n&&n&1^f(n/2)

Try it online!

PHP, 39 bytes

for(;$n=&$argn;$n/=2)$p+=$n|0;echo$p%2;

Try it online!

C (gcc), 34 21 bytes

Saved 13 bytes thanks to Arnauld!!!

f(n){n=n&&!f(n&n-1);}

Try it online!

• 21 bytes by counting bits recursively. – Arnauld Feb 11 at 23:32
• @Arnauld Wow! That's down right diabolical - thanks! :-) – Noodle9 Feb 12 at 4:41

¤ä^

Try it here