Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: [SOLVED] XOR like function in C/C++

  1. #1
    Join Date
    Feb 2008
    Location
    readlink("/proc/self/exe"
    Beans
    1,120
    Distro
    Ubuntu Development Release

    Question [SOLVED] XOR like function in C/C++

    Question:

    if i want to xor 2 values in C/C++, i can do this like this using:
    ( (p || q) && !(p && q) )
    which is equivalent to p xor q.

    How do i implement an xor function, that looks like p xor q?
    Example:
    Code:
    int main()
    {
         bool a,b;
         a=true; b=true;
         if (a xor b) 
              printf("true\n");
         else
              printf("false\n");
         return EXIT_SUCCESS ;
    }
    I can of course do a function like:
    Code:
    bool xor(bool p, bool q)
    {
         return ( (p || q) && !(p && q) ) 
    }
    But then the function call looks like:
    if( xor(a,b) )

    and i want: if ( a xor b )

    Can I do that at all in C/C++?

  2. #2
    Join Date
    Jun 2007
    Beans
    427

    Re: XOR like function in C/C++

    I don't think you can do that in C++ itself. C++ does have operator overloading, but that's only for particular operators. You can't create any operator of your choosing. My suggestion would be to create some preprocessor syntax that gets parsed before getting handed off to the compiler. That way you can have your simple function, but it will convert to C++ code at compile time.

  3. #3
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: XOR like function in C/C++

    ^ is the xor operator in C/C++
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  4. #4
    Join Date
    Jun 2007
    Beans
    427

    Re: XOR like function in C/C++

    Oh damn, it hurts. Hurts to the CORE. I was wrong. Damn.

  5. #5
    Join Date
    Feb 2008
    Location
    readlink("/proc/self/exe"
    Beans
    1,120
    Distro
    Ubuntu Development Release

    Red face Re: XOR like function in C/C++

    Quote Originally Posted by slavik View Post
    ^ is the xor operator in C/C++
    Oh, i thought ^ is only bitwise. Obviously that also works non bitwise; funny

    But it would still be interesting to know how to do a function that looks like:
    argument1 OPERAND argument2
    Last edited by WitchCraft; February 15th, 2008 at 02:58 AM.

  6. #6
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: XOR like function in C/C++

    Quote Originally Posted by WitchCraft View Post
    Oh, i thought ^ is only bitwise. Obviously that also works non bitwise; funny

    But it would still be interesting to know how to do a function that looks like:
    argument1 OPERAND argument2
    while in C you won't be able to do that (unless you change the language/compiler) in C++ you can overload the ^ operator to work on other things.

    (un)fortunately you cannot define your own operators in C/C++ (whether this is good or not can be debated till infinity).
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  7. #7
    Join Date
    Nov 2004
    Location
    Portland, Or, USA
    Beans
    289
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: XOR like function in C/C++

    Ummm, Depends on the types you are using...

    in C a bitwise xor is just ^

    thus a ^ b is a xor b

    unfortunately you can't overload ^ with

    bool operator^ (bool p, bool q)

    As at least one of the data types needs to be user defined...

    Z.

  8. #8
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: XOR like function in C/C++

    Quote Originally Posted by Zwack View Post
    Ummm, Depends on the types you are using...

    in C a bitwise xor is just ^

    thus a ^ b is a xor b

    unfortunately you can't overload ^ with

    bool operator^ (bool p, bool q)

    As at least one of the data types needs to be user defined...

    Z.
    I am pretty sure operator^(bool,bool) is defined already
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  9. #9
    Join Date
    Nov 2004
    Location
    Portland, Or, USA
    Beans
    289
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: XOR like function in C/C++

    I'd be surprised if bool ^ bool isn't defined... But my point is more that you can't really redefine it for built in types. So if you want a function that sits in the middle like a binary operator then you have to be redefining a current operator for a user defined type.

    If you decided you wanted a function to operate on two built-in types and you wanted it to look like a binary operator you can't... But then how would the compiler know what precedence to give it anyway?

    a ^ b & c - d has a set precedence, it will always act in the same way...
    but supposing I defined a new operator "

    what order would a ^ b " c - d be done in? What should the expected result be? While quote(a ^ b, c -d) is something that does have a recognised precedence.

    Equally being able to overload operators for built-in types would be dangerous... After all if you've redefined ^ to being something else then later you use it because you need to xor something how are you ever going to track down the cause of your odd results?

    Z.
    Last edited by Zwack; February 15th, 2008 at 03:31 AM.

  10. #10
    Join Date
    Feb 2008
    Location
    readlink("/proc/self/exe"
    Beans
    1,120
    Distro
    Ubuntu Development Release

    Re: XOR like function in C/C++

    Quote Originally Posted by Zwack View Post
    a ^ b & c - d has a set precedence, it will always act in the same way...
    but supposing I defined a new operator "
    That would be simple if you could set the precedence for the operator.
    But i see, that's something for (C++)++.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •