I’m working on a hardware device controller that uses a protocol which has quite a peculiar way to define two address values: the protocol has two bytes used in a way where Address A takes 10 bits (most significant) and Address B 6 bits (least significant). My initial thought was to set up a memoryblock(2) and then copy the values from another memoryblock bit by bit, but I haven’t really found a way to do that since most of the memoryblock functions work on bytes, not bits. I tried this with Einhugur’s CopyBitsTo, but don’t seem to get anything on the target memoryblock. Is there any more straightforward way to do this?
to set the 5th bit of a byte, you can OR the byte value with 0%00010000
to clear the 5th bit of a byte, you can AND the byte value with 0%11101111
with 2 simple methods, then you have methods to deal with bits.
Function setAddress( addrA as UInt16, addrB as UInt8) as MemoryBlock
dim m as new memoryBlock(2)
m.UInt16Value(0)=Bitwise.ShiftLeft(addrA, 6)+BitAnd(&b00111111, adder)
Function getAddress(m as MemoryBlock, byRef addrA as UInt16, byRef addrB as UInt8)
if m.size>=2 then
dim address_a as UInt16 = mb.UInt16(0) \\ 32 dim address_b as UInt16 = mb.UInt16(0) and 31
off the top of my head, and not tested, but that should point you in the right direction assuming I understood you “specs”
Here’s my version:
dim mb as new MemoryBlock( 2 ) mb.StringValue( 0 ) = theBytesAsString mb.LittleEndian = false dim v as UInt16 = mb.UInt16Value( 0 ) dim firstAddress as UInt16 = v \\ ( 2 ^ 6 ) // Shift right by 6 bits dim secondAddress As UInt16 = v and &b111111 // Keep the last 6 bits
For code readability, you might prefer
Bitwise.ShiftRight( v, 6 )
Thanks guys, it’s all sorted now.