Skip to content

Modern Math Parity

Paul Squires edited this page Oct 19, 2026 · 5 revisions

Home › Math Library › Math Library › Parity

Parity

Returns 1 if an odd number of bits are set, or 0 if an even number are.

Syntax

Parity( byval x as T ) as int32

Notes

  • PopCount mod 2, computed by folding rather than by counting -- so it is cheaper when the count itself is not wanted.
  • The check digit behind a parity bit and the simplest error-detecting code there is.

Examples

#import "std/numeric"
using numeric

dim as uint32 x = &b1011
print Parity( x )            '' 1 -- three bits, odd

dim as uint32 y = &b1010
print Parity( y )            '' 0 -- two bits, even

print Parity( x ) = PopCount( x ) mod 2       '' -1 (true)

Library: Math Library
Category: Math Library
Documented under: Members

Clone this wiki locally