Friday, September 15th, 2006

Operators and, or and xor written in English: is this standard C++?

Filed under: — Daniel Lemire @ 14:15

Kamel was reviewing some code I wrote and through a question he asked, I realized that some code I wrote would not compile under Visual C++. Further investigations showed that the following is valid under GCC, but not under Visual C++:


#include
using std::cout;
using std::endl;
int main(int argv, char ** args)
{
int a = 7;
int b = 3;
cout << (a and b) << endl;
cout << (a or b) << endl;
cout << (a xor b) << endl;
return 0
}

Can anyone help us out? Is this correct code?

Update: It looks like you can get this result under Visual C++ by including “iso646.h”. It includes the following definitions:

#define and &&
#define and_eq &=
#define bitand &
#define bitor |
#define compl ~
#define not !
#define not_eq !=
#define or ||
#define or_eq |=
#define xor ^
#define xor_eq ^=

3 Comments »

  1. Even if valid, that’s so awful methinks, besides: which operation do you mean? bitwise? logical?

    Comment by Ignacio — 16/9/2006 @ 21:25

  2. These are standard in C++. If Visual C++ doesn’t accept them, it’s broken. “and” means &&, while & would be “bitand”.

    Comment by Anonymous — 19/9/2006 @ 13:54

  3. You should be able to use the following operators (logical then bitwise)…

    and = &&, &
    or = ||, |
    xor = ^

    …without having to include “iso646.h”. This syntax has always been part of ANSI C right from the outset; it also makes no difference which version of VC++ you are using.

    Just ensure that you start a command-line project, as opposed to a Windows-based project, in VC++. This should include the basic headers for you, and won’t bloat your code.

    Hope this helps.

    Comment by Duke Aaron D'Attention — 1/11/2006 @ 6:43

RSS feed for comments on this post.

Leave a comment

Warning: When entering a long comment, please ensure that you make copy of your text prior to submitting it. If the server should fail or if you hit a bug, you might lose your work. I am not responsible for your lost effort.

To spammers: I carefully review every single post and make sure that spam gets deleted. You are wasting your time if you are manually entering spam using this form. Read my terms of use to see what I consider to be abusive.

Example: I + II + IX= XII. Yes, you have to enter a roman numeral. (Answer must be in upper case.)

« Blog's main page

25 queries. 0.312 seconds. Valid XHTML

Powered by WordPress

Subscribe to this blog in a reader or by Email.