www.gibmonks.com

Main Page




Previous Page
Next Page

[Page 1207 (continued)]

24.4. 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.

Figure 24.3. Operator keyword alternatives to operator symbols.

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 <iso646.h> (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 <iostream>
 4  using std::boolalpha;
 5  using std::cout;
 6  using std::endl;
 7
 8  #include <iso646.h> // 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 << "\n\nLogical operator keywords:";
24     cout << "\n   a and a: " << ( a and a );
25     cout << "\n   a and b: " << ( a and b );
26     cout << "\n    a or a: " << ( a or a );
27     cout << "\n    a or b: " << ( a or b );
28     cout << "\n     not a: " << ( not a );
29     cout << "\n     not b: " << ( not b );
30     cout << "\na not_eq b: " << ( a not_eq b );
31
32     cout << "\n\nBitwise operator keywords:";
33     cout << "\nc bitand d: " << ( c bitand d );
34     cout << "\nc bit_or d: " << ( c bitor d );
35     cout << "\n   c xor d: " << ( c xor d );
36     cout << "\n   compl c: " << ( compl c );
37     cout << "\nc and_eq d: " << ( c and_eq d );
38     cout << "\n c or_eq d: " << ( c or_eq d );
39     cout << "\nc xor_eq d: " << ( c xor_eq d ) << endl;
40     return 0;
41  } // end main

 a = true; b = false; c = 2; d = 3

 Logical operator keywords:
    a and a: true
    a and b: false
     a or a: true
     a or b: true
      not a: false
      not b: true
 a not_eq b: true

 Bitwise operator keywords:
 c bitand d: 2
 c bit_or d: 3
    c xor d: 1
    compl c: -3
 c and_eq d: 2
  c or_eq d: 3
 c xor_eq d: 0


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.


[Page 1209]

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.


Previous Page
Next Page