Operator Keywords
The C++ standard provides operator keywords (Fig. 24.3) that can be used in place of several C++ operators. Operator keywords are useful for programmers who have keyboards that do not support certain characters such as !, &, ^, ~, |, etc.
Operator |
Operator keyword |
Description |
---|---|---|
Logical operator keywords |
||
&& |
and |
logical AND |
|| |
or |
logical OR |
! |
not |
logical NOT |
Inequality operator keyword |
||
!= |
not_eq |
inequality |
Bitwise operator keywords |
||
& |
bitand |
bitwise AND |
| |
bitor |
bitwise inclusive OR |
^ |
xor |
bitwise exclusive OR |
~ |
compl |
bitwise complement |
Bitwise assignment operator keywords |
||
&= |
and_eq |
bitwise AND assignment |
|= |
or_eq |
bitwise inclusive OR assignment |
^= |
xor_eq |
bitwise exclusive OR assignment |
Figure 24.4 demonstrates the operator keywords. This program was compiled with Microsoft Visual C++ .NET, which requires the header file (line 8) to use the operator keywords. In GNU C++, line 8 should be removed and the program should be compiled as follows:
g++ -foperator-names Fig24_04.cpp -o Fig24_04
Figure 24.4. Demonstrating the operator keywords.
(This item is displayed on pages 1208 - 1209 in the print version)
1 // Fig. 24.4: fig24_04.cpp 2 // Demonstrating operator keywords. 3 #include 4 using std::boolalpha; 5 using std::cout; 6 using std::endl; 7 8 #include // enables operator keywords in Microsoft Visual C++ 9 10 int main() 11 { 12 bool a = true; 13 bool b = false; 14 int c = 2; 15 int d = 3; 16 17 // sticky setting that causes bool values to display as true or false 18 cout << boolalpha; 19 20 cout << "a = " << a << "; b = " << b 21 << "; c = " << c << "; d = " << d; 22 23 cout << " Logical operator keywords:"; 24 cout << " a and a: " << ( a and a ); 25 cout << " a and b: " << ( a and b ); 26 cout << " a or a: " << ( a or a ); 27 cout << " a or b: " << ( a or b ); 28 cout << " not a: " << ( not a ); 29 cout << " not b: " << ( not b ); 30 cout << " a not_eq b: " << ( a not_eq b ); 31 32 cout << " Bitwise operator keywords:"; 33 cout << " c bitand d: " << ( c bitand d ); 34 cout << " c bit_or d: " << ( c bitor d ); 35 cout << " c xor d: " << ( c xor d ); 36 cout << " compl c: " << ( compl c ); 37 cout << " c and_eq d: " << ( c and_eq d ); 38 cout << " c or_eq d: " << ( c or_eq d ); 39 cout << " c xor_eq d: " << ( c xor_eq d ) << endl; 40 return 0; 41 } // end main
|
The compiler option -foperator-names indicates that the compiler should enable use of the operator keywords in Fig. 24.3. Other compilers may not require you to include a header file or to use a compiler option to enable support for these keywords. For example, the Borland C++ 5.6.4 compiler implicitly permits these keywords.
The program declares and initializes two bool variables and two integer variables (lines 1215). Logical operations (lines 2430) are performed with bool variables a and b using the various logical operator keywords. Bitwise operations (lines 3339) are performed with the int variables c and d using the various bitwise operator keywords. The result of each operation is output.