Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python bitwise operations on C data types?

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'
>>>
like image 813
tMC Avatar asked Dec 27 '22 15:12

tMC


2 Answers

Try foo.value << 1

or to update foo,

foo.value <<= 1
like image 181
michel-slm Avatar answered Jan 14 '23 12:01

michel-slm


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

foo.value << 1
like image 35
Roberto Bonvallet Avatar answered Jan 14 '23 12:01

Roberto Bonvallet