Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Can you manipulate bits and memory directly using Python? If yes, wouldn't that be a best option to dealing with performance-critical spots than C bindings?

share|improve this question
Based on what I've read about python, I think this is the one thing that python can't do, almost by design. –  joshin4colours Mar 10 '12 at 3:39
Manipulating bits and memory is low level, but that doesn't mean it will improve performance much. –  Pubby Mar 10 '12 at 4:26
You can write your own extension in C and there manipulate bits and memory. docs.python.org/extending Are you sure you are cpu-bound and need this? –  gruszczy Mar 10 '12 at 13:48
I think it’s more gain to not bit-twiddle but use a C extension than to use bit-twiddling in Python. In fact, it may make the code slower. If you’re really into performance, consider using LLVM (possibly as JIT if the instructions are dynamic) but note that this makes your code much less portable. –  mirabilos Dec 6 '13 at 13:05
add comment

1 Answer 1

Bit manipulation in Python is possible (if you mean the standard bit operations like AND OR XOR etc.), but you cannot change arbitary memory by design. You can use Cython (http://cython.org/) however, which is a extended dialect of Python which compiles to C.

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.