0xFFFFFFFF in a P14P int

35 views
Skip to first unread message

GilRoss

unread,
Apr 2, 2013, 2:20:06 PM4/2/13
to python-o...@googlegroups.com
I would like to load 0xFFFFFFFF into a P14P int. When I try this, I get a value error. It is fine if I use -1, but I don't want to represent these bit masks as negative numbers. This makes for poor reading. Is there any way I can load a P14P int with 0xFFFFFFFF and make the code readable?

Thanks,
Gil

pir...@gmail.com

unread,
Apr 2, 2013, 2:43:11 PM4/2/13
to python-o...@googlegroups.com
0xFFFFFFFF Is ugly in C, and more in Python. It's way better to use
int mask = ~0x0

2013/4/2 GilRoss <gilber...@gmail.com>:
> --
> --
> You are subscribed to the "python-on-a-chip" (or p14p for short) Google
> Group.
> Site: http://groups.google.com/group/python-on-a-chip
>
> ---
> You received this message because you are subscribed to the Google Groups
> "python-on-a-chip" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to python-on-a-ch...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--
"Si quieres viajar alrededor del mundo y ser invitado a hablar en un
monton de sitios diferentes, simplemente escribe un sistema operativo
Unix."
– Linus Tordvals, creador del sistema operativo Linux

Marek Novák

unread,
Apr 2, 2013, 6:22:41 PM4/2/13
to python-o...@googlegroups.com
Is not ~0x0 evaluated runtime whereas 0xffffffff is a literal constant? 
But maybe Gil does not worry about the speed... 

Dean Hall

unread,
Apr 2, 2013, 9:17:36 PM4/2/13
to python-o...@googlegroups.com
The nature of the problem is that PyMite uses C signed 32-bit integers to hold Python integers
AND
Python tries to make hex constants keep their sign. This means Python will attempt to turn 0xFFFFFFFF into 4294967295 which cannot be represented in a signed 32-bit value. Python tries to coerce the value into Python's Long type. PyMite gives you an error.

To answer Marek's question: It's easy to check what the CPython "compiler" does using Python's interactive prompt:

>>> import dis
>>> def foo():
... a = 0xFFFFFFFF
... b = ~0x0
...
>>> dis.disco(foo.__code__)
2 0 LOAD_CONST 1 (4294967295)
3 STORE_FAST 0 (a)

3 6 LOAD_CONST 3 (-1)
9 STORE_FAST 1 (b)
12 LOAD_CONST 0 (None)
15 RETURN_VALUE

As you can see the "compiler" turns "~0x0" into the constant "-1".
So there is no execution penalty.

!!Dean

pir...@gmail.com

unread,
Apr 3, 2013, 3:05:33 AM4/3/13
to python-o...@googlegroups.com

Now I'm worried... Have I been doing wrong all the time or not?

Sent from my Android cell phone, please forgive the lack of format on the text, and my fat thumbs :-P

Reply all
Reply to author
Forward
0 new messages