# 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 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. Commented Feb 11, 2020 at 21:47
• @Arnauld Expecting the opposite output does make a substantial difference in some of the answers, though. Commented Feb 11, 2020 at 22:02
• Inverting the output is not really enough to justify a new challenge.
– Jo King
Commented Feb 11, 2020 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. Commented Feb 11, 2020 at 22:03
• @S.S.Anne Seems like there's nothing to highlight in there, but fixed anyway. :-) Commented Feb 11, 2020 at 22:07
• Hmm... That base 3 solution is interesting. Any chance that it could be implemented without converting to a string? Commented Feb 11, 2020 at 23:11

# Jelly, 3 bytes

BSḂ


Try it online! Or see the test-suite.

### How?

BSḂ - Link: non-negative integer
B   - to binary
S  - sum
Ḃ - least-significant-bit

• Interesting. +1 for the explanation. Commented Feb 11, 2020 at 21:31
• B^/ is another 3 Commented Feb 11, 2020 at 22:08
• On mobile it looks like BS[]. Stupid font encodings... Commented Feb 11, 2020 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

read*,i
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. Commented Feb 11, 2020 at 23:32
• @Arnauld Wow! That's down right diabolical - thanks! :-) Commented Feb 12, 2020 at 4:41

# Japt-h!, 3 bytes

¤ä^


Try it here