Can bitwise operators have undefined behavior?

Bitwise operators ( ~ , & , | and ^ ) work with the bitwise representation of their advanced operands. Can such operations cause undefined behavior?

For example, the ~ operator ~ defined this way in the C standard:

6.5.3.3 Unary arithmetic operators

The result of the ~ operator is the bitwise complement of its (advanced) operand (that is, each bit as a result is set if and only if the corresponding bit in the converted operand is not specified). Entire promotions are performed in the operand, and the result is of an advanced type. If the advanced type is an unsigned type, the expression ~E equivalent to the maximum value represented in this dash minus E

On all architectures, ~0 creates a pattern bit with the sign bit set to 1 , and all value bits set to 1 . In a single-add architecture, this representation is negative zero. Could this bitmap be a trap representation?

Are there other examples of undefined behavior that include simple bitwise operators for more general architectures?

+11
c ++ c language-lawyer bitwise-operators ones-complement
Sep 24 '17 at 7:33
source share
1 answer

For one complement system, the possibilities of trap values ​​are clearly indicated for those that do not support negative zeros in integer characters ( C11 6.2.6.2p4 ):

If the implementation does not support negative zeros, the behavior of the operators &, |, ^, ~, <<and β†’ with operands that will give such a value is undefined.

And again, one supplement system is not quite ordinary; like GCC does not support!

C11 implies that aspects defined by the implementation and undefined are only allowed for signed types (C11 6.5p4).

+9
Sep 24 '17 at 7:57
source share



All Articles