unsigned integer?

Discussion in 'Python' started by hg, Mar 10, 2007.

  1. hg

    hg Guest

    Dan Bishop wrote:

    > On Mar 10, 11:32 am, "Jack" <> wrote:
    >> This is a naive question:
    >>
    >> "%u" % -3
    >>
    >> I expect it to print 3. But it still print -3.
    >>
    >> Also, if I have an int, I can convert it to unsigned int in C:
    >> int i = -3;
    >> int ui = (unsigned int)i;
    >>
    >> Is there a way to do this in Python?

    >
    > def unsigned(n):
    > return n & 0xFFFFFFFF



    or abs(-1) ?
    hg, Mar 10, 2007
    #1
    1. Advertising

  2. hg

    Jack Guest

    This is a naive question:

    "%u" % -3

    I expect it to print 3. But it still print -3.

    Also, if I have an int, I can convert it to unsigned int in C:
    int i = -3;
    int ui = (unsigned int)i;

    Is there a way to do this in Python?
    Jack, Mar 10, 2007
    #2
    1. Advertising

  3. hg

    Dan Bishop Guest

    On Mar 10, 11:32 am, "Jack" <> wrote:
    > This is a naive question:
    >
    > "%u" % -3
    >
    > I expect it to print 3. But it still print -3.
    >
    > Also, if I have an int, I can convert it to unsigned int in C:
    > int i = -3;
    > int ui = (unsigned int)i;
    >
    > Is there a way to do this in Python?


    def unsigned(n):
    return n & 0xFFFFFFFF
    Dan Bishop, Mar 10, 2007
    #3
  4. hg

    Duncan Booth Guest

    "Jack" <> wrote:

    > This is a naive question:
    >
    > "%u" % -3
    >
    > I expect it to print 3. But it still print -3.


    Internally it uses the C runtime to format the number, but if the number
    you ask it to print unsigned is negative it uses %d instead of %u. I have
    no idea if it is actually possibly to get a different output for %d versus
    %u.

    >
    > Also, if I have an int, I can convert it to unsigned int in C:
    > int i = -3;
    > int ui = (unsigned int)i;
    >
    > Is there a way to do this in Python?
    >

    Depeneding on how exactly you want it converted:

    i = -3
    ui = abs(i)
    print ui
    ui = (i & 0xffff) # for 16 bit integers
    print ui
    ui = (i & 0xffffffff) # for 32 bit integers
    print ui
    ui = (i & 0xffffffffffffffff) # for 64 bit integers
    print ui
    ui = (i & 0xffffffffffffffffffffffffffffffff) # for 128 bit integers
    print ui

    which gives the following output:

    3
    65533
    4294967293
    18446744073709551613
    340282366920938463463374607431768211453

    There isn't a unique way to convert a Python integer to an unsigned value
    which is why the %u format string cannot do anything other than print the
    value. Personally I'd have expected the Python one to either print the
    absolute value or throw an exception, but I guess making it an alias for %d
    kind of makes sense as well.
    Duncan Booth, Mar 10, 2007
    #4
  5. hg

    Dan Bishop Guest

    On Mar 10, 11:50 am, Duncan Booth <>
    wrote:
    > "Jack" <> wrote:
    > > This is a naive question:

    >
    > > "%u" % -3

    >
    > > I expect it to print 3. But it still print -3.

    >
    > Internally it uses the C runtime to format the number, but if the number
    > you ask it to print unsigned is negative it uses %d instead of %u. I have
    > no idea if it is actually possibly to get a different output for %d versus
    > %u.


    %u used to be different from %d, but it changed because of the int/
    long unification in Python 2.4.
    Dan Bishop, Mar 10, 2007
    #5
  6. hg

    Duncan Booth Guest

    "Dan Bishop" <> wrote:

    > On Mar 10, 11:50 am, Duncan Booth <>
    > wrote:
    >> "Jack" <> wrote:
    >> > This is a naive question:

    >>
    >> > "%u" % -3

    >>
    >> > I expect it to print 3. But it still print -3.

    >>
    >> Internally it uses the C runtime to format the number, but if the
    >> number you ask it to print unsigned is negative it uses %d instead of
    >> %u. I have no idea if it is actually possibly to get a different
    >> output for %d versus %u.

    >
    > %u used to be different from %d, but it changed because of the int/
    > long unification in Python 2.4.
    >

    Yes, I guessed that was it.

    The implementation is identical when the value is negative but still
    different when the integer is non-negative which is why I questioned
    whether it was actually possible to get different output. If not perhaps
    both the implementation and the documentation should be simplified.
    Duncan Booth, Mar 10, 2007
    #6
  7. hg

    Jack Guest

    Thanks for all the replies. Because I want to convert an int,
    Dan's function actually does it well.

    "Jack" <> wrote in message
    news:...
    > This is a naive question:
    >
    > "%u" % -3
    >
    > I expect it to print 3. But it still print -3.
    >
    > Also, if I have an int, I can convert it to unsigned int in C:
    > int i = -3;
    > int ui = (unsigned int)i;
    >
    > Is there a way to do this in Python?
    >
    Jack, Mar 10, 2007
    #7
  8. hg

    Paul Rubin Guest

    "Jack" <> writes:
    > Also, if I have an int, I can convert it to unsigned int in C:
    > int i = -3;
    > int ui = (unsigned int)i;


    I just tried it:

    main() {
    int i = -3;
    unsigned int ui = i;
    printf("%d\n", ui);
    }

    prints -3. What do you want the conversion to do? If you want
    the absolute value, use abs().
    Paul Rubin, Mar 10, 2007
    #8
  9. En Sat, 10 Mar 2007 16:26:08 -0300, Paul Rubin
    <"http://phr.cx"@NOSPAM.invalid> escribió:

    > "Jack" <> writes:
    >> Also, if I have an int, I can convert it to unsigned int in C:
    >> int i = -3;
    >> int ui = (unsigned int)i;

    >
    > I just tried it:
    >
    > main() {
    > int i = -3;
    > unsigned int ui = i;
    > printf("%d\n", ui);
    > }
    >
    > prints -3. What do you want the conversion to do? If you want
    > the absolute value, use abs().


    Try again with "%u". Passing i or ui makes no difference, both push the
    same value on the stack. C relies on the format string to interpret the
    arguments.

    --
    Gabriel Genellina
    Gabriel Genellina, Mar 10, 2007
    #9
  10. hg

    Paul Rubin Guest

    "Gabriel Genellina" <> writes:
    > Try again with "%u". Passing i or ui makes no difference, both push
    > the same value on the stack. C relies on the format string to
    > interpret the arguments.


    If you use %u you get a very large positive value, not +3.
    Paul Rubin, Mar 10, 2007
    #10
  11. En Sat, 10 Mar 2007 20:26:13 -0300, Paul Rubin
    <"http://phr.cx"@NOSPAM.invalid> escribió:

    > "Gabriel Genellina" <> writes:
    >> Try again with "%u". Passing i or ui makes no difference, both push
    >> the same value on the stack. C relies on the format string to
    >> interpret the arguments.

    >
    > If you use %u you get a very large positive value, not +3.


    Exactly, and that's the right value. (unsigned int)(-3) isn't +3.

    --
    Gabriel Genellina
    Gabriel Genellina, Mar 10, 2007
    #11
  12. hg

    Paul Rubin Guest

    "Gabriel Genellina" <> writes:
    > > If you use %u you get a very large positive value, not +3.

    > Exactly, and that's the right value. (unsigned int)(-3) isn't +3.


    The OP specified that the expected result was 3.
    Paul Rubin, Mar 11, 2007
    #12
  13. Paul Rubin wrote:

    > The OP specified that the expected result was 3.


    But that's not what he'd get with his C conversion ;)

    Regards,


    Björn

    --
    BOFH excuse #348:

    We're on Token Ring, and it looks like the token got loose.
    Bjoern Schliessmann, Mar 11, 2007
    #13
  14. hg wrote:
    > Dan Bishop wrote:


    >> def unsigned(n):
    >> return n & 0xFFFFFFFF

    >
    > or abs(-1) ?


    Nah! Bitwise operators are cool. ;)

    Though ANDing won't make the int unsigned.

    Regards,


    Björn

    --
    BOFH excuse #23:

    improperly oriented keyboard
    Bjoern Schliessmann, Mar 11, 2007
    #14
  15. En Sat, 10 Mar 2007 21:04:00 -0300, Paul Rubin
    <"http://phr.cx"@NOSPAM.invalid> escribió:

    > "Gabriel Genellina" <> writes:
    >> > If you use %u you get a very large positive value, not +3.

    >> Exactly, and that's the right value. (unsigned int)(-3) isn't +3.

    >
    > The OP specified that the expected result was 3.


    Ouch! Yes, sorry, I overlooked it. And the C code just made things more
    confusing.

    --
    Gabriel Genellina
    Gabriel Genellina, Mar 11, 2007
    #15
    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. Timo Freiberger
    Replies:
    3
    Views:
    942
    Bob Hairgrove
    Oct 30, 2004
  2. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    674
    Eric Sosman
    Jul 8, 2003
  3. Replies:
    1
    Views:
    450
    Diez B. Roggisch
    Jun 1, 2005
  4. Replies:
    14
    Views:
    2,095
    CBFalconer
    Jun 18, 2005
  5. pozz
    Replies:
    12
    Views:
    738
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page