gogole
gogole

Reputation: 151

bitwise operator variations in C++

I read C++ provides additional operators to the usual &,|, and ! which are "and","or" and "not" respectively, plus they come with automatic short circuiting properties where applicable.

I would like to use these operators in my code but the compiler interprets them as identifiers and throws an error.

I am using Visual C++ 2008 Express Edition with SP1. How do I activate these operators to use in my code?

Upvotes: 1

Views: 594

Answers (2)

Alex Martelli
Alex Martelli

Reputation: 882751

The traditional C++ spelling [*] (just like in C) is && for "logical", short-circuit and, || for "logical", short-circuit or. ! is "logical" not (of course it doesn't short-circuit: what ever would that mean?!-). The bitwise versions are &, |, ~.

According to the C++ standard, the spellings you like (and, or, and so on) should also be implemented, but apparently popular compilers disobey that rule. However you should be able to #include <ciso646> or #include <iso646.h> to hack around that via macros -- see this page, and if your favorite compiler is missing these header files, just create them the obvious way, i.e.,

#define and &&
#define or ||

and so on. (Kudos and gratitude to the commenters for making me research the issue better and find this out!)

Upvotes: 2

Michael Burr
Michael Burr

Reputation: 340516

If you want to have the 'and', 'or', 'xor', etc keyword versions of the operators made available in MSVC++ then you either have to use the '/Za' option to disable extensions or you have to include iso646.h.

Upvotes: 4

Related Questions