4

Is it possible to do bitwise operations on C data types in Python?

Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import c_uint8
>>> foo = c_uint8(4)
>>> foo << 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for <<: 'c_ubyte' and 'int'
>>>
1
  • perhaps try int(foo) << 1. Or cast 1 into a `c_ubyte Commented Aug 18, 2011 at 18:55

3 Answers 3

4

Try foo.value << 1

or to update foo,

foo.value <<= 1
Sign up to request clarification or add additional context in comments.

Comments

4

This works, but it isn't done at the C level:

foo.value << 1

Comments

2
from ctypes import c_uint8
foo = c_uint8(4)
print foo.value << 1

will print 8 as you want. To change foo, use

foo.value <<= 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.