1

I have a bitstring.Bitarray and want to read from a certain position to another position. I have the int variable length in a for loop, so for example I have:

length = 2 and my Bitarray looks something like:

msgstr = bitstring.BitArray(0b11110011001111110)

id = bitstring.BitArray()
m = 0
while 5 != m:
   /////////////
   Length changes in value part of Code
   /////////////
   x = 0
   if m == 0:
       while length != x:
           id.append = msgstr[x] #msgstr is the BitArray that needs to be read
           x = x + 1
   m = m + 1

I then want to read the first two bits and convert them into an int, so that I have: id == 3 And for the next round when length has changed in value it should start from the third bit etc.

2 Answers2

1

The code inside your loop only does anything if m == 0, but then you increment m, so m is only 0 the first time through the loop. The rest of the times you go through your loop, it doesn't seem to actually be doing anything.

Also, where you say

id.append =  msgstr[x]

you probably actually want

id.append(msgstr[x])

It also seems like you might benefit from using Python's slice notation.

Community
  • 1
  • 1
Alex von Brandenfels
  • 1,608
  • 15
  • 25
0

I do not understand exactly what you goal is but do you had a look at https://wiki.python.org/moin/BitManipulation ?

mepi0011
  • 25
  • 7