Borland Online And The Cobb Group Present:


August, 1995 - Vol. 2 No. 8

Improving code readability - Using alternative tokens for bitwise and logical operators

One of the reasons for the popularity of Borland C++ is its ability to deal with low-level operations clearly and efficiently. For example, C++ provides several built-in operators for modifying the bit pattern in a given data structure.

Unfortunately, it's easy to confuse these operators with those that test for logical relationships or for equality/inequality. (For example, < looks like <<, and = looks like ==.) If you've enabled all the standard Borland C++ warnings, the compiler will warn you about such things as making an assignment in a conditional expression. However, many of us turn these warnings off as soon as we begin a new project, believing that the warnings don't represent truly dangerous coding practices.

In fact, confusion over these operators has been great enough to cause the American National Standards Institute (ANSI) C++ committee to consider alternative tokens for several of the bitwise and logical operators. (If you want to know more about the current standardization effort, see Keeping up with the latest changes to the C++ standard below.)

Table A summarizes the new tokens and their corresponding operators. Unfortunately, Borland C++ doesn't yet support these new tokens directly.

Table A: New bitwise and logical operator tokens
New Operators Common Operators
and &&
bitor |
or ||
xor ^
compl ~
bitand &
and_eq &=
or_eq |=
xor_eq ^=
not !
not_eq !=

If you want to begin using these tokens right away, you can simply create a header file that defines each of the new tokens as a macro for the corresponding operator. Once you've created this file, you can add it to your source files using an #include directive, and then you can begin using the new tokens.

Return to the Borland C++ Developer's Journal index

Subscribe to the Borland C++ Developer's Journal


Copyright (c) 1996 The Cobb Group, a division of Ziff-Davis Publishing Company. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff-Davis Publishing Company is prohibited. The Cobb Group and The Cobb Group logo are trademarks of Ziff-Davis Publishing Company.