This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Binary Bitwise operators with higher precedence.
- From: Marcio Afonso Arimura Fialho <maaf at dea dot inpe dot br>
- To: gcc at gcc dot gnu dot org
- Date: Fri, 30 Jul 2004 16:48:09 -0100
- Subject: Binary Bitwise operators with higher precedence.
C/C++ are great languages, but, despite this, they have some
kudgles and flaws, as would be expected for an old language.
One particular, and very annoying flaw (in my viewpoint) is the
fact that bitwise operators ( '&' , '|' and '^' ) have lower
precedence than relational and equality operators ('<', '<=', '>', '>=',
'==' and '!='), forcing the programmer to enclose a bitwise operation in
parentheses whenever he/she wants to compare the result from a bitwise
operation with some value. Eg. to check if bits 3,2 of variable 'status'
are set and bit 0 of a0 is clear, the programmer must write:
[Code]
if ( (status & 0x0D) == 0x0C)
{ /* Do whathever should be done when bits 3,2 of status are set
and bit 0 of status is clear */
}
[/Code]
Life would be much easier for a C/C++ programmer interfacing with
hardware if the enclosing parentheses around the bitwise operation could
be removed.
One option would be to redefine the language in order to increase
the precedence level of bitwise operators at least higher than the
precedence of the relational and (un)equality operators. But this is not
really an option, since it would render the new language not backward
compatible, plus, it could also lead to confusion. And also, it may be
useful in some circunstances to have bitwise operators with lower
precedence than relational operators, eg. in optimizations where logical
ands ors, etc are implemented via bitwise operators.
I've also though in other ways to resolve this problem, including
adding some GCC specific keyword that would increase the precedence
level of the bitwise operators, but this could create confusions,
creating conditions for potential bugs. So after thinking about this
many times, I think I've found out a good solution, much better than the
previous solutions (see below):
A proposed solution:
Instead of redefining the precedence of the existing bitwise
operators, create three new operators. Eg. ( '&%', '^%' and '|%' ,
where '&%' would be bitwise and; '^%' would be bitwise xor; and '|%'
would be bitwise or). These new operators would be identical to the
old-ones, except that they would have a higher precedence than
relational and (un)equality operators, so the code above could be
rewritten as:
[Code]
if (status &% 0x0D == 0x0C)
{ /* Do whathever should be done when bits 3,2 of status are set
and bit 0 of status is clear */
}
[/Code]
This is much clearer to read than the code with old bit-wise operators.
This approach also has the advantage of leading the traditional bitwise
operators with their traditional meaning, plus making a compiler which
incorporates this feature backward compatible with code written for
older compilers.
All the text above means is that these new bitwise operators should have
a precedence at least as high as the precedence of shift operators ('>>'
and '<<') and arithmetic operators (eg, add, subtract, product,
division and remainder), but I'm unsure of which exact precedence order
would be the best, in terms of easier coding and easier remembering. (I
mean, for example, should the operator '&%' has a greater precedence
than '+' or not?).
I would like to know if is there anything similar in GCC or if anyone
has proposed something similar in the past. If not, I would like to open
a discussion about these proposed bitwise operators. After comming to a
consensus I would like this feature to be added to GCC as an extension,
and hopefully, this feature be added to ISO-C standard as well in the
forthcoming years (if it's proven to be a useful feature).
Also I've proposed the symbol '%' postponed to the tradicional operators
'&','^' and '|' because it was the first thing that came out to my mind,
after looking to my keyboard. But I did some tests with gcc (version
3.2.1) and these operators '&%', '&^' and '&|' doesn't exist. Can you
think of any side effect that might ocurr if these operators are added
to GCC? Any suggestion for replacements of '%'?
Best Regards,
Márcio A. A. Fialho
------------------------------------------------
Appendix A:
C/C++ Operator Precedence List - Source: Borland's Turbo C++ 3.1
on-line help
(I hope I'm not breaking any Copyright law)
(Category) (operator) (description)
1. Highest
() Function call
[] Array subscript
-> C++ indirect component selector
:: C++ scope access/resolution
. C++ direct component selector
2. Unary
! Logical negation (NOT)
~ Bitwise (1's) complement
+ Unary plus
- Unary minus
++ Preincrement or postincrement
-- Predecrement or postdecrement
& Address
* Indirection
sizeof (returns size of operand, in bytes)
new (dynamically allocates C++ storage)
delete (dynamically deallocates C++ storage)
3. Multiplicative
* Multiply
/ Divide
% Remainder (modulus)
4. Member access
.* C++ dereference
->* C++ dereference
5. Additive
+ Binary plus
- Binary minus
6. Shift
<< Shift left
>> Shift right
7. Relational
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
8. Equality
== Equal to
!= Not equal to
9. Bitwise operator
& Bitwise AND
10. Bitwise operator
^ Bitwise XOR
11. Bitwise operator
| Bitwise OR
12. Logical operator
&& Logical AND
13. Logical operator
|| Logical OR
14. Conditional
?: (a ? x : y means "if a then x, else y")
15. Assignment
= Simple assignment
*= Assign product
/= Assign quotient
%= Assign remainder (modulus)
+= Assign sum
-= Assign difference
&= Assign bitwise AND
^= Assign bitwise XOR
|= Assign bitwise OR
<<= Assign left shift
>>= Assign right shift
16. Comma
, Evaluate
---------------------
Proposed precedence order for the new bitwise operators ( &% ^%
and |% ): Anywhere below precedence level 2 (unary) and above
precedence level 7 (relational). Exact position to be debated.
Exact naming of new bitwise operators also subject to debate, unless
everybody agress with my suggestion.