Thread: What does ^ do?

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    11

    What does ^ do?

    I'm a noob at C programming and I have a line of code

    "P5_0^=1;"

    which basically toggles an LED on a board I'm using when a switch is pressed.

    Can someone explain to me what it is the ^ symbol!

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    ^ is bitwise exclusive or (called xor for short). Every bit in a ^ b is unset if the corresponding bits in a and b are equal, and set if they are unequal.

    Like all operator-assign operators, "a ^= b" has the same net effect as "a = a ^ b".

    Like all bitwise operators, both operands for ^ have to be of integral type. Preferably unsigned, since the result is undefined for signed types with negative values.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed