Parity Check Implementation in Java
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 Java as in every other language.
- ✓java.lang.Integer.bitCount(int) and java.lang.Long.bitCount(long), both auto-imported and both returning int; parity is Integer.bitCount(x) & 1.
- ✓Reviewers want Integer.bitCount(x) & 1 and reject a hand-rolled loop, because the JDK method is a HotSpot intrinsic and the loop is not.
- ✓The Java trap: Shift distances are masked, not clamped: for int only the low five bits of the count are used, so x >>> 32 returns x unchanged rather than 0, and for long only the low six.
- ✓Integer.bitCount and Long.bitCount are HotSpot intrinsics that collapse to one POPCNT when the CPU reports support and UsePopCountInstruction is on; otherwise the SWAR body written in Integer runs.
What Parity Check actually is
Parity Check in Java: what you actually reach for
Java implementation
public final class Parity {
private Parity() {}
/** 1 when an odd number of bits are set. */
public static int of(long x) {
return Long.bitCount(x) & 1;
}
/** Branch-free fold. Note >>> at every single step. */
public static int fold(long x) {
x ^= x >>> 32;
x ^= x >>> 16;
x ^= x >>> 8;
x ^= x >>> 4;
return (0x6996 >>> (int) (x & 0xF)) & 1;
}
/** & 0xFF because byte is signed and widens with sign extension. */
public static int ofBytes(byte[] buf) {
int acc = 0;
for (byte b : buf) {
acc ^= b & 0xFF;
}
return Integer.bitCount(acc) & 1;
}
public static void main(String[] args) {
System.out.println(of(0b1011L)); // 1
System.out.println(of(-1L)); // 0, 64 bits set
System.out.println(fold(-1L)); // 0
}
}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 Java-specific trap
Performance in Java, beyond the O(1)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Java type or library should I use for Parity Check?
java.lang.Integer.bitCount(int) and java.lang.Long.bitCount(long), both auto-imported and both returning int; parity is Integer.bitCount(x) & 1. Their neighbours are highestOneBit, numberOfLeadingZeros, reverse, rotateLeft, and Integer.toUnsignedLong/compareUnsigned, which exist because Java has no unsigned integer types. java.util.BitSet.cardinality() is the popcount for an arbitrarily long bitmap. There is no parity method anywhere in the JDK; >>> is the unsigned-shift substitute.
What is the most common Java mistake when implementing Parity Check?
Shift distances are masked, not clamped: for int only the low five bits of the count are used, so x >>> 32 returns x unchanged rather than 0, and for long only the low six. A 64-bit fold written against an int accumulator therefore opens with a silent no-op and reports the parity of half the value. The second Java-only edge is signedness of the small types: byte and short are signed and widen with sign extension, so Integer.bitCount(b) on a negative byte counts 24 extra ones. Parity survives that by luck (24 is even); popcount does not.
How does Parity Check actually perform in Java?
Integer.bitCount and Long.bitCount are HotSpot intrinsics that collapse to one POPCNT when the CPU reports support and UsePopCountInstruction is on; otherwise the SWAR body written in Integer runs. Either way the first calls execute interpreted, then C1, before C2 ever applies the intrinsic, so a Stopwatch-style microbenchmark without warmup measures the interpreter. That is what JMH exists for. Boxing is the other tax: a Map<Integer, Integer> parity cache allocates for every key outside the -128..127 Integer cache. 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? GitHub Copilot 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.