Shop OBEX P1 Docs P2 Docs Learn Events
Why does this work in one form, but not the other (bit writing) — Parallax Forums

Why does this work in one form, but not the other (bit writing)

Jorge PJorge P Posts: 385
edited 2011-11-23 00:15 in Propeller 1
When I use serial terminal to test this
PUB BitToByte(yourByte, position0to7, value)
    '' sets the bit value at position0to7 in yourByte
    ''
    '' the tmpBits are actualy byte datatypes since there is no bit datatype

    ClearVars
    
    tmpBit8  := yourByte << 7 
    tmpBit0  := tmpBit8 >> 7
    
    tmpBit9  := yourByte << 6
    tmpBit1  := tmpBit9 >> 7
    
    tmpBit10 := yourByte << 5
    tmpBit2  := tmpBit10 >> 7
    
    tmpBit11 := yourByte << 4
    tmpBit3  := tmpBit11 >> 7
    
    tmpBit12 := yourByte << 3
    tmpBit4  := tmpBit12 >> 7
    
    tmpBit13 := yourByte << 2
    tmpBit5  := tmpBit13 >> 7
    
    tmpBit14 := yourByte << 1
    tmpBit6  := tmpBit14 >> 7
    
    tmpBit15 := yourByte << 0
    tmpBit7  := tmpBit15 >> 7
    
    '
    case position0to7
      0:
        tmpBit0 := value                 
      1:
        tmpBit1 := value
      2:
        tmpBit2 := value
      3:
        tmpBit3 := value
      4:
        tmpBit4 := value
      5:
        tmpBit5 := value
      6:
        tmpBit6 := value
      7:
        tmpBit7 := value
      OTHER:
        ' TODO: return something indicating invalid position

      
    byteOut0 := tmpBit7 << 7
    byteOut0 := byteOut0 | (tmpBit6 << 6)
    byteOut0 := byteOut0 | (tmpBit5 << 5)
    byteOut0 := byteOut0 | (tmpBit4 << 4)
    byteOut0 := byteOut0 | (tmpBit3 << 3)
    byteOut0 := byteOut0 | (tmpBit2 << 2)
    byteOut0 := byteOut0 | (tmpBit1 << 1)
    byteOut0 := byteOut0 | (tmpBit0 << 0)
      
    return byteOut0 


PUB BitToNibble(yourNibble, position0to3, value)
    '' sets the bit value at position0to3 in yourNibble
    ''
    '' value is either one or zero!
    '' the tmpBits are actualy byte datatypes, since there is no bit datatype
    '' 
    ClearVars
    
    
    tmpBit8  := yourNibble << 3 
    tmpBit0  := tmpBit8 >> 3
    
    tmpBit9  := yourNibble << 2
    tmpBit1  := tmpBit9 >> 3
    
    tmpBit10 := yourNibble << 1
    tmpBit2  := tmpBit10 >> 3
    
    tmpBit11 := yourNibble << 0
    tmpBit3  := tmpBit11 >> 3
    
    
    '
    case position0to3
      0:
        tmpBit0 := value                 
      1:
        tmpBit1 := value
      2:
        tmpBit2 := value
      3:
        tmpBit3 := value
      OTHER:
        ' TODO: return something indicating invalid position

    NibbleOut0 := tmpBit3 << 3
    NibbleOut0 := NibbleOut0 | (tmpBit2 << 2)
    NibbleOut0 := NibbleOut0 | (tmpBit1 << 1)
    NibbleOut0 := NibbleOut0 | (tmpBit0 << 0)
      
    return NibbleOut0

PUB BitToByte(yourByte, position0to7, value) works as expected, however when writing a value of 0 (zero) to PUB BitToNibble(yourNibble, position0to3, value) nothing is changed? But BitToNibble with a value of one works as expected.

I was trying to do all this on my own but this has me a bit confused, pun intended, what is wrong here.

NOTE: ClearVars just zeros out the variable used.

Thanks in advance for your help.
Jorge

Comments

  • Jorge PJorge P Posts: 385
    edited 2011-11-22 23:25
    In case it is needed, here is the output from my serial terminal:
    Testing Bit writing functions!!!

    Beginning Byte to alter is 11111100
    Set individual bits to 0
    11111100
    11111100
    11111000
    11110100
    11101100
    11011100
    10111100
    01111100
    Set individual bits to 1
    11111101
    11111110
    11111100
    11111100
    11111100
    11111100
    11111100
    11111100


    Beginning Nibble to alter is 00000110
    Set individual bits to 0
    00000110
    00000110
    00000110
    00000110
    Set individual bits to 1
    00000111
    00000110
    00000110
    00001110
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-11-22 23:49
    To be honest I think that it won't work with bytes either. You only found a test-case where it worked by chance. Try other bit-patterns for yourByte! Like (%0011_0101).

    But these functions are way to much "complex thinking". You have bit operations in spin which will reduce the amount of code needed to do the same thing a lot:

    BitToByte:
      byteOut:=yourByte
      if value
        byteOut:=byteOut | |<position0to7
      else
        byteOut:=byteOut & !|<position0to7
      return byteOut 
    
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-11-23 00:02
    If you want to make your code work, you can remove the tmpbit8-15 lines and for the rest do:

    tmpbit0 := yourByte & 1
    tmpbit1 := (yourByte >> 1) & 1
    tmpbit2 := (yourByte >> 2) & 1
    ...


    Ok .... thought about it ... bytes should work .... but what is the effect of moving left and then right again? It is cutting away all bits not needed. But this only works if you shift the bits out of the byte. This is what's not working in the nibble-function. You shift the nibble to the left but your variable is a byte. So no bits are removed, they are still there in the upper nibble of the byte. If you now shift back you don't get an extracted bit.
    In your nibble function you should also shift left 7,6,5,4 bits and then shift right 7 bits.

    But that's only to explain what's going wrong ... it's still not efficient ;o)
  • Jorge PJorge P Posts: 385
    edited 2011-11-23 00:08
    First off, thanks for showing me how to do it properly! Much appreciated. I am fairly new to spin. However, with the code I posted, I just went through and tried other bit patterns for yourByte including the one you suggested, it seems to work. But regardless I will stick with the code you showed me for my app.

    NOTE: to anyone using SPINtax in Greasemonkey with firefox, it leaves out some code in the example above after the |< operator and only displays | |.
  • Jorge PJorge P Posts: 385
    edited 2011-11-23 00:15
    Yes the returned byte I will be shifting at a later time since I will also be working with the high nibble, and I forgot about what you mentioned about the nibbles being shifted left 7, 6, 5, 4 because it is actually a byte. It took me a while to figure that out in my other functions, Thanks again for your help. Marking the thread as solved.
Sign In or Register to comment.