Parity Check Implementation in Go
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Parity Check runs in O(1) time and O(1) space, in Go as in every other language.
- ✓math/bits is the home: bits.OnesCount64(x uint64) int, plus OnesCount8/16/32 and the platform-width OnesCount(uint).
- ✓Reviewers want the unsigned conversion visible at the boundary, bits.OnesCount64(uint64(n)) written where the reader can see it, not buried in a helper.
- ✓The Go trap: Go's shifts are unbounded by specification: x >> 64 on a uint64 evaluates to 0, not to x as in Java and C#, and not to a panic as in a Rust debug build.
- ✓The compiler intrinsifies bits.OnesCount64, so it never appears in a profile as a call: on arm64 it becomes VCNT plus ADDV, and on amd64 it emits POPCNT behind a check of a runtime CPU-feature flag, so the baseline build carries one predictable branch per call that raising the GOAMD64 level removes.
What Parity Check actually is
Parity Check in Go: what you actually reach for
Go implementation
package parity
import "math/bits"
// Parity64 reports 1 when an odd number of bits are set.
func Parity64(x uint64) int {
return bits.OnesCount64(x) & 1
}
// Fold is the branch-free equivalent, for targets where POPCNT
// is not available and the CPU-feature branch is not worth it.
func Fold(x uint64) int {
x ^= x >> 32
x ^= x >> 16
x ^= x >> 8
x ^= x >> 4
return int((0x6996 >> (x & 0xF)) & 1)
}
// ParityBytes collapses a buffer to a single parity bit.
func ParityBytes(b []byte) int {
var acc byte
for _, v := range b {
acc ^= v
}
return bits.OnesCount8(acc) & 1
}
// ParityInt is the explicit, reviewable conversion boundary.
func ParityInt(n int) int {
return Parity64(uint64(n))
}Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Go-specific trap
Performance in Go, beyond the O(1)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Go type or library should I use for Parity Check?
math/bits is the home: bits.OnesCount64(x uint64) int, plus OnesCount8/16/32 and the platform-width OnesCount(uint). Parity is bits.OnesCount64(x) & 1. The package also carries Reverse64, RotateLeft64, LeadingZeros64, TrailingZeros64, Len64 and the wide-arithmetic helpers Add64/Mul64/Div64. There is no parity function and no signed variant: every entry point takes an unsigned type, which forces you to make the conversion decision explicitly at the call site.
What is the most common Go mistake when implementing Parity Check?
Go's shifts are unbounded by specification: x >> 64 on a uint64 evaluates to 0, not to x as in Java and C#, and not to a panic as in a Rust debug build. Code ported from C or Java that leans on a masked shift count therefore yields zero, and the fold then reports parity 0 for every input, which looks like a plausible answer. The conversion side mirrors it: the constant expression uint64(-1) is a compile error, but uint64(n) for a variable n == -1 compiles and sign-extends to 64 ones. A negative shift count panics at run time.
How does Parity Check actually perform in Go?
The compiler intrinsifies bits.OnesCount64, so it never appears in a profile as a call: on arm64 it becomes VCNT plus ADDV, and on amd64 it emits POPCNT behind a check of a runtime CPU-feature flag, so the baseline build carries one predictable branch per call that raising the GOAMD64 level removes. A [256]byte table pays a bounds check unless the index is provably masked, plus a potential cache miss the intrinsic never suffers. Escape analysis is irrelevant here: everything stays in registers. Time complexity is O(1) and space is O(1).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialStudying for this? Devin free tiers and academic plans are laid out on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.