Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I limit bit number in the integer variable in Python?

I want to realize IDEA algorithm in Python. In Python we have no limits for variable size, but I need limit bit number in the integer number, for example, to do cyclic left shift. What do you advise?

like image 306
Siarhei Fedartsou Avatar asked Feb 26 '23 19:02

Siarhei Fedartsou


1 Answers

One way is to use the BitVector library.

Example of use:

>>> from BitVector import BitVector
>>> bv = BitVector(intVal = 0x13A5, size = 32)
>>> print bv
00000000000000000001001110100101
>>> bv << 6                            #does a cyclic left shift
>>> print bv
00000000000001001110100101000000
>>> bv[0] = 1
>>> print bv
10000000000001001110100101000000
>>> bv << 3                            #cyclic shift again, should be more apparent
>>> print bv
00000000001001110100101000000100
like image 136
Muhammad Alkarouri Avatar answered Mar 27 '23 05:03

Muhammad Alkarouri