Calculates and returns the parity of a number.

    The parity of a number is ``1`` if the number has an odd number of ones
    in its binary representation, otherwise ``0``.

    Args:
      n (int): the number whose parity to calculate

    Returns:
      ``1`` if the number has an odd number of ones, otherwise ``0``.

    Raises:
      ValueError: if ``n`` is less than ``0``.

def calculate_parity(n):
    """Calculates and returns the parity of a number.

    The parity of a number is ``1`` if the number has an odd number of ones
    in its binary representation, otherwise ``0``.

    Args:
      n (int): the number whose parity to calculate

    Returns:
      ``1`` if the number has an odd number of ones, otherwise ``0``.

    Raises:
      ValueError: if ``n`` is less than ``0``.
    """
    if not is_natural(n):
        raise ValueError('Expected n to be a positive integer.')

    y = 0
    n = abs(n)
    while n:
        y += n & 1
        n = n >> 1
    return y & 1