Why does this work in one form, but not the other (bit writing)
Jorge P
Posts: 385
When I use serial terminal to test this
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
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
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:
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)
NOTE: to anyone using SPINtax in Greasemonkey with firefox, it leaves out some code in the example above after the |< operator and only displays | |.