Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the definition of bitwise operators in C++?
    primarykey
    data
    text
    <p>According to the standard, operator &lt;&lt; yields an Undefined Behavior for a negative signed first operand.</p> <p><strong>C++11 5.8.2</strong></p> <pre><code>The value of E1 &lt;&lt; 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 </code></pre> <p>This is understandable as the layout of integers in memory is implementation defined.</p> <p><strong>C++11 3.9.1.7</strong></p> <pre><code>this International Standard permits 2’s complement, 1’s complement and signed magnitude representations for integral types. </code></pre> <p>On the other hand, the standard do not seem to define exactly what bitwise &amp; | and ^ should do.</p> <p><strong>C++11 5.11 Bitwise AND operator</strong></p> <pre><code>and-expression: equality-expression and-expression &amp; 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. </code></pre> <p><strong>C++11 5.12 Bitwise exclusive OR operator</strong></p> <pre><code>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. </code></pre> <p><strong>C++11 5.13 Bitwise inclusive OR operator</strong></p> <pre><code>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. </code></pre> <p>The definition of those operators completely eludes me. Is it somewhere else in the standard ? Is the result implementation defined for signed integer ?</p> <p>As an example let's look at this code:</p> <pre><code>signed char a=-1; signed char b=3; signed char c=a&amp;b; </code></pre> <p>With 2's complement, a is 1111 1111, and b is 0000 0011. Finally c equals 0000 0011 (+3).</p> <p>With 1's complement, a is 1111 1110, and b is 0000 0011. Does c equals 0000 0010 (+2) ?</p> <p>With sign-magnitude, a is 1000 0001, and b is 0000 0011. Does c equals 0000 0001 (+1) ?</p> <p>If you have access to platforms using 1's complement or sign-magnitude, what is the result on those platforms ?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload