Search code examples
operators

What does the ^= operator do?


Hello i was wondering if someone could explain what the ^= operator is doing in this c program? The program is being written for an arm architecture.

#include <stdint.h>
#include <pru_cfg.h>

volatile register uint32_t __R30;
volatile register uint32_t __R31;

void main() {
    volatile uint32_t gpo;

    /* Clear GPO pins */
    gpo = (__R30 & 0xFFFF0000);
    __R30 = gpo;
    
    while(1) {
        gpo = __R30;
        gpo ^= 0xF;
        __R30 = gpo;
        __delay_cycles(100000000); // half-second delay
    }
}

If you require any other info, let me know, Thank you


Solution

  • In C, ^ is the bitwise exclusive or, and

    gpo ^= 0xF;
    

    is equivalent to

    gpo = gpo ^ 0xF;
    

    See https://en.cppreference.com/w/c/language/operator_assignment#Compound_assignment for more details about it.