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

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

  1. Miki Tebeka

    Miki Tebeka Guest

    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):
    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

    HTH.

    Bye.
    --
    -------------------------------------------------------------------------
    Miki Tebeka <>
    The only difference between children and adults is the price of the toys.
    Miki Tebeka, Jun 14, 2004
    #1
    1. Advertising

  2. 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
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sarmin kho
    Replies:
    2
    Views:
    825
    A. Lloyd Flanagan
    Jun 15, 2004
  2. Ioannis Vranos
    Replies:
    11
    Views:
    758
    Ioannis Vranos
    Mar 28, 2008
  3. Ioannis Vranos

    Padding bits and char, unsigned char, signed char

    Ioannis Vranos, Mar 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    612
    Ben Bacarisse
    Mar 29, 2008
  4. Rob1bureau
    Replies:
    1
    Views:
    807
    joris
    Feb 27, 2010
  5. pozz
    Replies:
    12
    Views:
    738
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page