Skip to content Skip to sidebar Skip to footer

Pack Numbers Into A Bitset (python,bitwise Operations)

The PIC microcontroller has a dead simple instruction set format. Each instruction is exactly 14 bits long, composed of a variety of numbers at differing bit lengths. I am trying t

Solution 1:

Your shifts are wrong.

You are shifting by the index of the top-most bit, which isn't right. You must shift by the index of the lowermost (rightmost) bit in each field.

So it should be:

deffileRegOp(opcode, d, f):
  return (opcode << 8) | (d << 7) | f

This gives, with some editing to add padding zeros on the left:

>>> bin(fileRegOp(1,True,15))
'0b00000110001111'

Of course, it might be sensical to also limit-check the arguments.

Post a Comment for "Pack Numbers Into A Bitset (python,bitwise Operations)"