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

Discussion in 'Python' started by Miki Tebeka, Jun 14, 2004.

1. Miki TebekaGuest

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 <>
The only difference between children and adults is the price of the toys.
Miki Tebeka, Jun 14, 2004

2. Marcin 'Qrczak' KowalczykGuest

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://qrnik.knm.org.pl/~qrczak/
Marcin 'Qrczak' Kowalczyk, Jun 14, 2004