XOR Question

This is a discussion on XOR Question within the C Programming forums, part of the General Programming Boards category; I was reading a thread about XOR, and having to do this for my current program had a question on ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    36

    XOR Question

    I was reading a thread about XOR, and having to do this for my current program had a question on it.

    I know && and || are part of C,

    so isnt there a way to have an XOR without doing several basic operations?(Like a command I havent yet seen)

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>so isnt there a way to have an XOR without doing several basic operations?(Like a command I havent yet seen)
    Sure, the ^ operator performs an XOR and ^= does an XOR then assignment :-)
    Code:
    /*
    How to set an int to 0 without saying
      var = 0;
    */
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      int a = rand();
    
      printf("%d\n", a);
      a ^= a;
      printf("%d\n", a);
    
      return 0;
    }
    *Cela*

  3. #3
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    the != operator.

    All that xor does is return true if either operand is true, but not both. That's exactly what != does (considering you are dealing with just a single value representing true and a single value representing false ).

    If you want the bitwise version of xor it actually has its own operator which is ^

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21