Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   unsigned integer? (http://www.velocityreviews.com/forums/t483214-unsigned-integer.html)

 hg 03-10-2007 10:58 AM

Re: unsigned integer?

Dan Bishop wrote:

> On Mar 10, 11:32 am, "Jack" <nos...@invalid.com> 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) ?

 Jack 03-10-2007 05:32 PM

unsigned integer?

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?

 Dan Bishop 03-10-2007 05:47 PM

Re: unsigned integer?

On Mar 10, 11:32 am, "Jack" <nos...@invalid.com> 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

 Duncan Booth 03-10-2007 05:50 PM

Re: unsigned integer?

"Jack" <nospam@invalid.com> 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.

 Dan Bishop 03-10-2007 06:21 PM

Re: unsigned integer?

On Mar 10, 11:50 am, Duncan Booth <duncan.bo...@invalid.invalid>
wrote:
> "Jack" <nos...@invalid.com> 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.

 Duncan Booth 03-10-2007 06:34 PM

Re: unsigned integer?

"Dan Bishop" <danb_83@yahoo.com> wrote:

> On Mar 10, 11:50 am, Duncan Booth <duncan.bo...@invalid.invalid>
> wrote:
>> "Jack" <nos...@invalid.com> 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.

 Jack 03-10-2007 07:17 PM

Re: unsigned integer?

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

"Jack" <nospam@invalid.com> wrote in message
news:I9Kdndp49PJLdm_YnZ2dnUVZ_t-mnZ2d@comcast.com...
> 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?
>

 Paul Rubin 03-10-2007 07:26 PM

Re: unsigned integer?

"Jack" <nospam@invalid.com> 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().

 Gabriel Genellina 03-10-2007 11:08 PM

Re: unsigned integer?

En Sat, 10 Mar 2007 16:26:08 -0300, Paul Rubin
<"http://phr.cx"@NOSPAM.invalid> escribió:

> "Jack" <nospam@invalid.com> 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

 Paul Rubin 03-10-2007 11:26 PM

Re: unsigned integer?

"Gabriel Genellina" <gagsl-py2@yahoo.com.ar> 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.

All times are GMT. The time now is 01:24 PM.