7
\$\begingroup\$

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

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.

Sandbox link (10k+)

\$\endgroup\$
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – RGS Feb 11 at 18:35
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – Jeff Zeitlin Feb 11 at 19:03
  • 2
    \$\begingroup\$ How do you define "counting the number of set bits"? \$\endgroup\$ – Jeff Zeitlin Feb 11 at 19:15
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – Post Rock Garf Hunter Feb 11 at 21:03
  • 5
    \$\begingroup\$ Inverting the output is not really enough to justify a new challenge. \$\endgroup\$ – Jo King Feb 11 at 23:43

13 Answers 13

3
\$\begingroup\$

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!

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

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
\$\endgroup\$
  • \$\begingroup\$ Interesting. +1 for the explanation. \$\endgroup\$ – S.S. Anne Feb 11 at 21:31
  • 1
    \$\begingroup\$ B^/ is another 3 \$\endgroup\$ – Nick Kennedy Feb 11 at 22:08
  • \$\begingroup\$ On mobile it looks like BS[]. Stupid font encodings... \$\endgroup\$ – S.S. Anne Feb 11 at 23:29
2
\$\begingroup\$

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)
\$\endgroup\$
1
\$\begingroup\$

Python 2, 27 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Fortran (GFortran), 28 bytes

read*,i
print*,poppar(i)
end

Try it online!

Fortran has this as an in-built since F2008

\$\endgroup\$
1
\$\begingroup\$

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?
\$\endgroup\$
1
\$\begingroup\$

Oasis, 4 bytes

ES2%

Try it online!

Explanation

ES2%

 S    # the sum of...
E     # the binary digits...
  2%  # mod 2
\$\endgroup\$
1
\$\begingroup\$

Ruby, 19 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Python 3, 28 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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!

\$\endgroup\$
0
\$\begingroup\$

PHP, 39 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

C (gcc), 34 21 bytes

Saved 13 bytes thanks to Arnauld!!!

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

Try it online!

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

Japt -h!, 3 bytes

¤ä^

Try it here

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.