Skip to content

Instantly share code, notes, and snippets.

@schwehr
Last active May 27, 2016 16:21
Show Gist options
  • Save schwehr/389ca654caf87804195d7cabe4f4e3a6 to your computer and use it in GitHub Desktop.
Save schwehr/389ca654caf87804195d7cabe4f4e3a6 to your computer and use it in GitHub Desktop.
// Copyright 2014 Google Inc. All Rights Reserved
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <cstdio>
#include <iostream>
using namespace std;
#define TRUE 1
#define FALSE 0
int main(void) {
cout << (false ^ false ? "true" : "false") << "\n";
cout << (FALSE ^ FALSE ? "true" : "false") << "\n";
// != is xor
cout << (FALSE != FALSE ? "true" : "false") << "\n";
cout << (FALSE != TRUE ? "true" : "false") << "\n";
cout << (TRUE != FALSE ? "true" : "false") << "\n";
cout << (TRUE != TRUE ? "true" : "false") << "\n";
int iFalse = 0;
int iTrue1 = 1;
int iTrue2 = 2;
if (iTrue1) printf("True1!\n");
if (iTrue2) printf("True2!\n");
if (iTrue1 ^ iTrue2) printf("first try: XOR\n");
if ((!iTrue1) ^ (!iTrue2)) printf("second try: XOR\n");
cout << "xor value 0 ^ 0: " << (iFalse ^ iFalse) << "\n";
cout << "xor value 0 ^ 1: " << (iFalse ^ iTrue1) << "\n";
cout << "xor value 1 ^ 1: " << (iTrue1 ^ iTrue1) << "\n";
cout << "xor value 1 ^ 2: " << (iTrue1 ^ iTrue2) << "\n";
// Printing is still annoying.
cout << "false: " << false << "\n"; // 0
cout << "true: " << true << "\n"; // 1
}
/*
g++ -o xor -Wall -Wextra -O2 xor.cc -std=c++11 && ./xor
false
false
false
true
true
false
True1!
True2!
first try: XOR
xor value 0 ^ 0: 0
xor value 0 ^ 1: 1
xor value 1 ^ 1: 0
xor value 1 ^ 2: 3
false: 0
true: 1
g++ -o xor -Wall -Wextra -O2 xor.cc -std=c++98 && ./xor
false
false
false
true
true
false
True1!
True2!
first try: XOR
xor value 0 ^ 0: 0
xor value 0 ^ 1: 1
xor value 1 ^ 1: 0
xor value 1 ^ 2: 3
false: 0
true: 1
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment