0

I'm looking for a way to read, manipulate, and write specific ranges of (less than 8) bits in a byte array.

I want something like, let's say I want to work with the 6-8th bit of a byte array:

R0 = bytearray(8)

bits = readBits(R0, 6, 2) # bytearray, position, range
print(bits) # [0, 0] or maybe it's easier to work with a byte 
# with the irrelevant bits zeroed, you tell me
# do stuff ... XOR, AND, ETC
writeBits(R0, bits, 6) # target, source, position

I'm writing an x86-64 emulator in Python and some instructions in the General Purpose Registers can reference 2 bit segments of the 64 bit register, which I've been representing as bytearray(8). The smallest unit of a byte array seems to be a byte (go figure). Is there a way to select bits? Let's say I want to read, manipulate, and update the 6th-8th bits of an 8 byte object? What does that look like?

Maybe there's a more granular data structure I should be using rather than a byte array?

One answer to a similar question working with a hex string suggests:

i = int("140900793d002327", 16)
# getting bit at position 28 (counting from 0 from right)
i >> 28 & 1
# getting bits at position 24-27
bin(i >> 24 & 0b111)

But this code isn't explained super well, for example what does 0b111 do here and how do I use this approach dynamically to isolate any desired range of bits as with my imaginary readBits and writeBits funcs, rather than the hard-coded functionality here?

J.Todd
  • 707
  • 1
  • 12
  • 34
  • if you are writing a CPU emulator you should be familiar with bit shift operations (`>>`) and bit masking `&`, `0b111` is a literal int in binary (7 in decimal) – rioV8 Oct 29 '20 at 12:20
  • @rioV8 Not necessarily. I come from a data analysis, machine learning background. Also the emulator I'm writing is sort of light weight, with a specialized malware analysis purpose. I'm half way through reading Intel's Software Developer Manual Volumes 1 & 2 to remedy my deficiency on the topic of CPU emulation, and while I'm making progress toward understanding the components and inner workings, that doesn't mean I know the complexities of binary manipulation with various languages. – J.Todd Oct 29 '20 at 12:29
  • if you see Python code that contains operators you don't know you read the doc on Python operators. How simple do you want bit shifting to be explained other than what is already to be found with a simple search – rioV8 Oct 29 '20 at 13:00
  • @rioV8 I understand what bit shifting does, I wasnt sure it was necessary first of all. is it so unreasonable to imagine you can work with bits directly as an array structure, rather than manipulating a byte to isolate desired bits? Yes I realize there isnt addressing on the bit level, so it's not possible to create an object where individual bits are directly referenced, but we could work with an array of boolean values. Seems to me that would potentially cut out the computational cost of the bitwise operations. Maybe not, but not unreasonable to ask if there's a bit granularity data struct. – J.Todd Oct 29 '20 at 13:47
  • you can write a class `MyInteger` and override any operator you want and make bit addressing possible, but you still need to do the shift-masking-ORing inside the class – rioV8 Oct 29 '20 at 13:52

0 Answers0