標準によると、演算子 << は、負符号付きの最初のオペランドに対して未定義の動作を生成します。
C++11 5.8.2
The value of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are zero-
filled. If E1 has an unsigned type, the value of the result is E1 × 2 pow E2,
reduced modulo one more than the maximum value representable in the result type.
Otherwise, if E1 has a signed type and non-negative value, and E1 × 2 pow E2 is
representable in the result type, then that is the resulting value; otherwise,
the behavior is undefined
メモリ内の整数のレイアウトは実装定義であるため、これは理解できます。
C++11 3.9.1.7
this International Standard permits 2’s complement, 1’s complement and
signed magnitude representations for integral types.
一方、標準では、ビットごとの & | を正確に定義していないようです。そして^するべきです。
C++11 5.11 ビットごとの AND 演算子
and-expression:
equality-expression
and-expression & equality-expression
1 The usual arithmetic conversions are performed; the result is the bitwise
AND function of the operands. The operator applies only to integral
or unscoped enumeration operands.
C++11 5.12 ビット単位の排他的 OR 演算子
exclusive-or-expression:
and-expression
exclusive-or-expression ˆ and-expression
1 The usual arithmetic conversions are performed; the result is the bitwise
exclusive OR function of the operands. The operator applies only to integral
or unscoped enumeration operands.
C++11 5.13 ビット単位の包含 OR 演算子
inclusive-or-expression:
exclusive-or-expression
inclusive-or-expression | exclusive-or-expression
1 The usual arithmetic conversions are performed; the result is the bitwise
inclusive OR function of its operands. The operator applies only to integral
or unscoped enumeration operands.
これらの演算子の定義は、私には完全にわかりません。それは標準のどこかにありますか?符号付き整数に対して結果の実装が定義されていますか?
例として、次のコードを見てみましょう。
signed char a=-1;
signed char b=3;
signed char c=a&b;
2 の補数では、a は 1111 1111、b は 0000 0011 です。最終的に c は 0000 0011 (+3) に等しくなります。
1 の補数で、a は 1111 1110 で、b は 0000 0011 です。c は 0000 0010 (+2) に等しいですか?
符号の大きさでは、a は 1000 0001 で、b は 0000 0011 です。c は 0000 0001 (+1) に等しいですか?
1 の補数または符号の大きさを使用してプラットフォームにアクセスできる場合、それらのプラットフォームでの結果はどうなりますか?