def checksum(number, bits=4):
    """
    Calculate the checksum of a number.

    The checksum of length N is formed by splitting the number into
    bitstrings of N bits and performing a bitwise exclusive or on them.

    :param number: Number to generate the check
    :type number: int

    :return: Checksum of the number
    :rtype: int

    >>> checksum(0)
    0
    >>> checksum(0xFFF)
    15
    >>> checksum(0b01100111, 2)
    1

    """
    if bits == 0:
        return 0

    mask = (1 << bits) - 1
    chk = 0
    while number > 0:
        chk ^= (number & mask)
        number >>= bits
    return chk & mask