what about unsigned and signed 8 bits number, 16 bits, etc??

Miki Tebeka miki.tebeka at zoran.com
Mon Jun 14 16:50:36 CEST 2004

Hello sarmin,

>    When it is an integer number, what is the range of the integer number
>    and long integer number??
The range of int is the same as of the machine int (the C compiler that
was used to build Python). Note that in 2.4+ int overflow will
automatically become long.
Long numbers are a big as your computer memory can handle (try 1L<<10000).

>    do we have typecasting of 8bits or 16bits signed and unsigned number
>    in python?
Not that I know of. I use this simple class:
class SizedNum:
    def __init__(self, size, value=0):
        self.mask = (1 << size) - 1

    def set(self, value):
        self.value = value & self.mask
And also:
def sign_extend(num, size):
    '''Sign exten number who is `size' bits wide'''
    res = num & MASK(size - 1)
    # Positive
    if (num & (1L << (size - 1))) == 0:
        return res

    # Negative, 2's complement
    res = ~res
    res &= MASK(size - 1) 
    res += 1
    return -res


Miki Tebeka <miki.tebeka at zoran.com>
The only difference between children and adults is the price of the toys.

More information about the Python-list mailing list