hex and unsigned longs

Dan Bishop danb_83 at yahoo.com
Sat Apr 12 14:03:18 EDT 2003


igorr at ifi.uio.no (Igor V. Rafienko) wrote in message news:<xjvof3bu75x.fsf at margvisi.ifi.uio.no>...
> Hi,
> 
> The documentation for the built-in function hex() says that hex()
> always returns an unsigned literal. However, on my 2.2.1 python, I get
> the following results:
> 
> >>> hex( 1 )
>  '0x1'
> >>> hex( 1L )
>  '0x1L'
> >>> hex( -1 )
>  '0xffffffff'
> >>> hex( -1L )
>  '-0x1L'
> >>> 
> 
> The literal in the last case is obviously not unsigned. What am I
> missing? (I.e. why are longs treated differently than ints by hex?)

Because 0xffffffffL == 4294967295.

There's no wraparound for longs, so you can't represent a negative
long by an unsigned constant (unless it has an infinite number of
leading f's).




More information about the Python-list mailing list