Menu

Direct printing of 'bit' portion of byte fails if referenced by variable

18 hours ago
15 hours ago
  • c-conversion

    c-conversion - 18 hours ago

    I was struggling with a print statement in a program recently which wasn't working as I expected. I kept thinking I had my logic mixed up but eventually it turned out to be an oddity in the 'Print' statement.

    It may be 'expected' behaviour, if 'unexpected' for me.

    This works:

    Print MyByte.0
    
    Let MyByteBit = 0
    Let MyReferencedBit = MyByte.MyByteBit
    Print MyReferencedBit
    

    This does not work reliably:

    Print MyByte.MyByteBit
    

    It will print "1" only if the most significant bit of MyByte is set to 1.
    So if MyByte has a value > 127: Although this changed when I added an extra line to print spaces to the right of the decimal printed value? See the example program I used to test this.

    Let MyByte = 128
    Let MyByteBit = 0
    Print MyByte.MyByteBit 'Prints 1
    Let MyByteBit = 1
    Print MyByte.MyByteBit 'Prints 1
    ...
    Let MyByteBit = 7
    Print MyByte.MyByteBit 'Prints 1
    
    Let MyByte = 127
    Let MyByteBit = 0
    Print MyByte.MyByteBit 'Prints 0
    Let MyByteBit = 1
    Print MyByte.MyByteBit 'Prints 0
    ...
    Let MyByteBit = 7
    Print MyByte.MyByteBit 'Prints 0
    

    Test program used:

    #Chip 18F16Q41, 64
    
    #Option Explicit
    #Config CP=On
    
    'LCD connection settings
    #Define LCD_IO 4
    #Define LCD_SPEED FAST
    #Define LCD_NO_RW
    
    'Port assignments
    #Define LCD_RS        PortA.0
    #Define LCD_Enable    PortA.1
    
    #Define LCD_DB4       PortA.2
    #Define LCD_DB5       PortC.0
    #Define LCD_DB6       PortC.1
    #Define LCD_DB7       PortC.2
    
    Dim MyByte        As Byte
    Dim MyByteBit     As Byte
    Dim ReferencedBit As Bit
    CLS
    
    Let MyByte = 0
    
    Do
    
        Locate 0, 0
        Print MyByte.7 'These print as expected
        Print MyByte.6
        Print MyByte.5
        Print MyByte.4
        Print MyByte.3
        Print MyByte.2
        Print MyByte.1
        Print MyByte.0
    
        Locate 0, 13
        Print MyByte
        'Print "  "
        'With the above line commented out, prints "1" when MSB set
        'With the line used, prints "1" when LSB set?
    
        Locate 1,0
    
        Let MyByteBit = 7
        Repeat 8
            Print MyByte.MyByteBit 'This does not print as expected.
            Let MyByteBit = MyByteBit - 1
        End Repeat
    
        Let MyByteBit = 7
    
        Repeat 8
            Let ReferencedBit = MyByte.MyByteBit
            Print ReferencedBit    'This prints as expected
            Let MyByteBit = MyByteBit - 1
        End Repeat
    
        Let MyByte = MyByte + 1
    
        If MyByte < 1 Then
            Let MyByte = 1
        End If
        Wait 100 mS
    
    Loop
    

    Now I am aware of this, I'm using this in my program:

    If MyByte.MyByteBit = 1 Then
        Print "1"
    Else
        Print "0"
    End If
    

    Which is 100% reliable.
    At one point "Print MyByte.MyByteBit" printed nothing, not a "1", not a "0", nothing, as though the line was not even present. It was this behaviour that had me most confused.

    As I said, it may be that I was trying to do something that isn't possible and that is what actually should happen. I only report it as it gave me half a day of head scratching and confusion.

     

    Last edit: c-conversion 18 hours ago
  • Anobium

    Anobium - 15 hours ago

    Great insights.

    Print MyByte.MyByteBit is working as expected. And your logic is a good approach.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.