-3 % 2 is -1 in Rust. Not 1.

The remainder keeps the sign of the dividend, the way C does, so an odd negative number has a negative remainder. That single fact decides which parity check in this crate is correct, and the crate contains two parity checks that are both correct, for opposite reasons.

impl Solution {
    pub fn even_or_odd(n: i32) -> String {
        // Single modulo
        // match n % 2 {
        //     0 => "Even".to_string(),
        //     _ => "Odd".to_string(),
        // }
        // bitwise
        match n & 1 {
            1 => "Odd".to_string(),
            _ => "Even".to_string(),
        }
    }
}

Three ways to ask the same question

The shipped version uses n & 1. The commented-out one above it uses n % 2 matched against zero. The version I was about to write before I checked, and which is not in the file, uses n % 2 matched against one.

Here is what all three do across a range of inputs:

n=  4  shipped=Even   match-on-1=Even   match-on-0=Even
n=  7  shipped=Odd    match-on-1=Odd    match-on-0=Odd
n=  0  shipped=Even   match-on-1=Even   match-on-0=Even
n= -3  shipped=Odd    match-on-1=Even   match-on-0=Odd
n= -4  shipped=Even   match-on-1=Even   match-on-0=Even
n=  3  shipped=Odd    match-on-1=Odd    match-on-0=Odd

The third column is wrong exactly once, on -3. It says -3 is even. -3 % 2 is -1, the match on 1 does not fire, and everything falls into the _ arm, which is Even.

The bit does not care about the sign

-3 & 1 is 1 and -4 & 1 is 0. In two’s complement, the low bit of a number is its parity bit regardless of the sign, because the sign is the top bit and the value of the low bit is the only thing that decides whether the number is even. -3 is ...11111101 in binary, so its low bit is 1. i32::MIN is ...10000000, so its low bit is 0, and i32::MIN is even.

The bitwise form does not have a negative-input case, because there is no such thing.

Matching on zero is right for a different reason

The commented-out version matches on zero rather than on one. -3 % 2 is -1, and -1 is not zero, so the number falls into the _ arm, which is Odd. The sign of the remainder never reaches the answer, because the only question the match asks is whether the remainder is zero, and zero has no sign.

So the two correct implementations in this file are correct for structurally different reasons. One uses an operator whose result is sign-independent and matches on the bit that matters. The other uses a sign-dependent operator and matches on the one value the sign cannot change.

The version that breaks is the natural combination: the sign-dependent operator matched against the sign-dependent value. “The remainder is one, therefore the number is odd” is a perfectly reasonable sentence, and in Python it is a true sentence, because Python’s % is floor-mod and -3 % 2 is 1 there. A programmer moving from Python to Rust carries that sentence over and it stops being true.

Only negative inputs show it

The repo’s test suite is:

for (num, s) in [(4, "Even"), (7, "Odd"), (0, "Even")] {
    assert_eq!(Solution::even_or_odd(num), s)
}

Three non-negative inputs. Every one of them produces a non-negative remainder, so all three parity checks agree on all three, and the suite cannot see the difference at all.

The constraint is -10^9 <= n <= 10^9, so negative inputs are in scope. A single (-3, "Odd") would separate the three variants.

Neither of these can overflow

n % 2 cannot overflow for an i32. Rust’s % has exactly one overflow case, i32::MIN % -1, and rustc rejects that expression at compile time rather than letting it run. The divisor here is the constant 2, so the question does not arise.

n & 1 cannot overflow either, and it cannot panic. Bitwise operations on a signed integer in Rust are defined on the two’s complement representation, with no undefined behavior and no special case for negative values. The low bit is the low bit.

Both of those are properties worth having and neither of them is what makes this function interesting. What makes it interesting is one digit of the language that changes the meaning of a line of code, and it changes the meaning only in the half of the input range the tests do not reach.

What I keep from this one is a habit rather than a rule: when I write a parity check, I test the negative input first, because the entire difference lives there.


Next: I Reimplemented min()