Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: what about unsigned and signed 8 bits number, 16 bits, etc?? (http://www.velocityreviews.com/forums/t332097-re-what-about-unsigned-and-signed-8-bits-number-16-bits-etc.html)

 Miki Tebeka 06-14-2004 02:50 PM

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

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 <miki.tebeka@zoran.com>
The only difference between children and adults is the price of the toys.

 Marcin 'Qrczak' Kowalczyk 06-14-2004 03:19 PM

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

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
\__/ qrczak@knm.org.pl
^^ http://qrnik.knm.org.pl/~qrczak/

 All times are GMT. The time now is 05:35 PM.