Computational Parity Party!

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

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+)

S.S. Anne

Posted 2020-02-11T18:04:07.537

Reputation: 1 161

Question was closed 2020-02-11T23:43:43.140

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.240

1@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

Answers

3

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!

Arnauld

Posted 2020-02-11T18:04:07.537

Reputation: 111 334

I think you moved your code up above the lang mark, so it's not getting syntax highlighting. – S.S. Anne – 2020-02-11T22:03:16.470

@S.S.Anne Seems like there's nothing to highlight in there, but fixed anyway. :-) – Arnauld – 2020-02-11T22:07:15.713

Hmm... That base 3 solution is interesting. Any chance that it could be implemented without converting to a string? – S.S. Anne – 2020-02-11T23:11:20.987

2

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

Jonathan Allan

Posted 2020-02-11T18:04:07.537

Reputation: 67 804

Interesting. +1 for the explanation. – S.S. Anne – 2020-02-11T21:31:16.663

1B^/ is another 3 – Nick Kennedy – 2020-02-11T22:08:22.917

On mobile it looks like BS[]. Stupid font encodings... – S.S. Anne – 2020-02-11T23:29:09.263

2

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)

Jonathan Allan

Posted 2020-02-11T18:04:07.537

Reputation: 67 804

1

Python 2, 27 bytes

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

Try it online!

ovs

Posted 2020-02-11T18:04:07.537

Reputation: 21 408

1

Fortran (GFortran), 28 bytes

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

Try it online!

Fortran has this as an in-built since F2008

DeathIncarnate

Posted 2020-02-11T18:04:07.537

Reputation: 916

1

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?

mabel

Posted 2020-02-11T18:04:07.537

Reputation: 1 489

1

Oasis, 4 bytes

ES2%

Try it online!

Explanation

ES2%

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

mabel

Posted 2020-02-11T18:04:07.537

Reputation: 1 489

1

Ruby, 19 bytes

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

Try it online!

G B

Posted 2020-02-11T18:04:07.537

Reputation: 11 099

0

C (gcc), 34 21 bytes

Saved 13 bytes thanks to Arnauld!!!

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

Try it online!

Noodle9

Posted 2020-02-11T18:04:07.537

Reputation: 2 776

21 bytes by counting bits recursively. – Arnauld – 2020-02-11T23:32:15.587

@Arnauld Wow! That's down right diabolical - thanks! :-) – Noodle9 – 2020-02-12T04:41:21.613

0

Python 3, 28 bytes

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

Try it online!

Noodle9

Posted 2020-02-11T18:04:07.537

Reputation: 2 776

0

Shaggy

Posted 2020-02-11T18:04:07.537

Reputation: 24 623

0

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!

Shaggy

Posted 2020-02-11T18:04:07.537

Reputation: 24 623

0

PHP, 39 bytes

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

Try it online!

Guillermo Phillips

Posted 2020-02-11T18:04:07.537

Reputation: 561