def dec2bin(num, width=0):
    """
    >>> dec2bin(0, 8)
    '00000000'
    >>> dec2bin(57, 8)
    '00111001'
    >>> dec2bin(3, 10)
    '0000000011'
    >>> dec2bin(-23, 8)
    '11101001'
    >>> dec2bin(23, 8)
    '00010111'
    >>> dec2bin(256)
    '100000000'
    """
    if num < 0:
        if not width:
            raise ValueError('Width must be specified for negative numbers')
        num += 2 ** width
    binary = ''
    while num:
        if num & 1:
            binary += '1'
        else:
            binary += '0'
        num >>= 1
    if width:
        no_zeros = width - len(binary)
        if no_zeros < 0:
            raise OverflowError('A binary of width %d cannot fit %d' %
                    (width, num))
        binary += '0' * no_zeros
    if not binary:
        binary = '0'
    return binary[::-1]