Velocity Reviews > Re: what about unsigned and signed 8 bits number, 16 bits, etc??

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

Miki Tebeka
Guest
Posts: n/a

 06-14-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
self.set(value)

def set(self, value):
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 += 1
return -res

HTH.

Bye.
--
-------------------------------------------------------------------------
Miki Tebeka <(E-Mail Removed)>
The only difference between children and adults is the price of the toys.

Marcin 'Qrczak' Kowalczyk
Guest
Posts: n/a

 06-14-2004
On Mon, 14 Jun 2004 16:50:36 +0200, Miki Tebeka wrote:

> The range of int is the same as of the machine int (the C compiler that
> was used to build Python).

No, Python int corresponds to C long.

--
__("< Marcin Kowalczyk
\__/ http://www.velocityreviews.com/forums/(E-Mail Removed)
^^ http://qrnik.knm.org.pl/~qrczak/