Menu

New Compound Operators in GCBASIC: XOR= and OR=

Anobium
2025-01-12
2025-01-12
  • Anobium

    Anobium - 2025-01-12

    New Compound Operators in GCBASIC: XOR= and OR=

    Hello GCBASIC community!

    I am excited to announce that the latest compiler update now supports two new compound operators: XOR= and OR=. These join our existing compound operators (++, --, +=, -=) to provide more efficient bit manipulation capabilities.

    New Operators and Their Equivalents

    XOR= Operator

    The new compound XOR operator simplifies your code. These two code blocks do exactly the same thing:

    ; Using new XOR= operator
    myVar XOR= 0b00001111
    
    ; Equivalent traditional code
    myVar = myVar XOR 0b00001111
    

    Here's a real-world example showing both forms:

    ; Toggle an LED using XOR=
    PORTB.0 XOR= 1    'Toggles bit 0 of PORTB
    
    ; Same operation written longhand
    PORTB.0 = PORTB.0 XOR 1
    

    OR= Operator

    portStatus OR= 0b10000000   'Sets bit 7 while preserving other bits
    flags OR= STATUS           'Combines status bits with existing flags
    

    Practical Applications

    XOR= Use Cases:

    1. Toggle bits without affecting others - perfect for flipping specific LED states or inverting signal lines
    2. Implement simple encryption - XOR with a key pattern for basic data scrambling
    3. Swap values without temporary storage:
    ; Using XOR=
    a XOR= b
    b XOR= a
    a XOR= b    'Values are now swapped!
    
    ; Equivalent code without XOR=
    a = a XOR b
    b = b XOR a
    a = a XOR b
    

    OR= Use Cases:

    1. Set specific bits while preserving others - ideal for configuring port settings
    2. Combine status flags from different operations
    3. Enable features without disrupting existing settings

    Example: Shift Register Control

    Here's how these operators simplify shift register handling:

    #define LATCH_PIN PORTC.0
    #define DATA_PIN  PORTC.1
    #define CLOCK_PIN PORTC.2
    
    Sub UpdateDisplay(in displayPattern as Byte)
        ; Clear latch
        PORTC = PORTC AND NOT (1 << LATCH_PIN)
    
        ; Shift out 8 bits
         Dim bitcount as Byte
        For bitCount = 0 to 7
            ; Clear data pin
            PORTC = PORTC AND NOT (1 << DATA_PIN)
    
            ; Set data bit if needed
            If displayPattern.7 Then
                PORTC OR= (1 << DATA_PIN)  'Set only DATA_PIN
            End If
    
            ; Toggle clock using XOR=
            PORTC XOR= (1 << CLOCK_PIN)    'Toggle CLOCK_PIN
    
            ; Same toggle without XOR= would be:
            'PORTC = PORTC XOR (1 << CLOCK_PIN)
    
            displayPattern = displayPattern << 1
        Next
    
        ; Set latch to update display
        PORTC OR= (1 << LATCH_PIN)
    End Sub
    

    Bit Manipulation Examples

    Here's a practical example showing how XOR= can simplify LED patterns:

    ; Flash alternate LEDs on PORTB
    Sub FlashLEDs
        ; Toggle bits 0,2,4,6
        PORTB XOR= 0b01010101
    
        ; Equivalent without XOR=
        'PORTB = PORTB XOR 0b01010101
    
        ; Toggle bits 1,3,5,7 on next pass
        PORTB XOR= 0b10101010
    
        ; Equivalent without XOR=
        'PORTB = PORTB XOR 0b10101010
    End Sub
    

    Compatibility

    These new operators are available in the latest compiler version. All existing code using the traditional compound operators (++, --, +=, -=) continues to work as before.

    Give these new operators a try and let me know how they improve your code! As always, I appreciate your feedback and suggestions for future enhancements.

    Enjoy

    Evan

     
  • stan cartwright

    stan cartwright - 2025-01-12

    remember ssd1309 oscilloscope xor mode to erase graphics by redrawing them?

     
    • Anobium

      Anobium - 2025-01-12

      Yes, I still use it! And, the sprite demo!

       

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.