Shop OBEX P1 Docs P2 Docs Learn Events
Confused on what I am getting with highbit() and lowbit() — Parallax Forums

Confused on what I am getting with highbit() and lowbit()

John KauffmanJohn Kauffman Posts: 653
edited 2013-02-11 14:23 in BASIC Stamp
I haven't been able to get what I expect when using myByte.highbit() and MyByte.lowbit(). (short demo program attached).
I've been searching fora but must not be using proper terms.

I'm trying to get the bits out of a byte representing an ASCII char for example: g is 01100111. But counting up or down, high or low first, I can't seem to get the right pattern when I loop through the bits. I thought it would be:

'?????
' sample: h = 01101000
' = msb...lsb
' = HighBit(0)...HighBit(3) LowBit(0)...LowBit(3)
' ?????

I would appreciate a pointer to where I can learn the syntax to get a given bit as I count it off in the BIN representation of a byte

thanksBitstreamDemoVer01.bs2

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2013-02-10 20:48
    I think the problem you're running into is that .highbit refers to the highest bit of the storage unit accessed, so nibble.highbit would be bit 3 of the 4-bit nibble, byte.highbit would be bit 7 of the 8-bit byte, and word.highbit would be bit 15 of the 16-bit word. When you add a bit number to this, the bits count from right to left, so the only valid subscript is zero. nibble.highbit(1), byte.highbit(1), and word.highbit(1) do not exist.

    I think your loop that references .lowbit should be ok. That accesses bits 0, 1, 2, and 3 in that order. If you want to access the next 4 bits, use .lowbit(BitCounter+4).

    There are all sorts of other ways to do this. I would probably use the least significant bit for the output datastream and shift the character right one bit for each loop to get all 8 bits in order. That's the way most hardware does it.

    If you can afford to use a word to hold the character, this loop acts like most hardware:
    tempWord = character | $100  ' mark the end of the character to be sent
    do
       carrier = tempWord.lowbit  ' send the least significant bit
       tempWord = tempWord >> 1  ' shift the whole thing right one bit
    loop while tempWord > 1  ' continue until the mark bit is the only thing left
    
  • John KauffmanJohn Kauffman Posts: 653
    edited 2013-02-11 14:23
    Thanks, Mike. That made it completely clear & my code now works.
Sign In or Register to comment.