Reputation: 64925
Is it guaranteed that (2 ^ 32) == 34
?
Upvotes: 5
Views: 3685
Reputation: 6647
yes
Or at least for the unedited version of the question when it was written as:
2 ^ 32 == 34
Given that the relational operator ==
has a higher precedence than bitwise XOR ^
, the expression is evaluated as:
2 ^ (32 == 34)
that is: 2 ^ 0
which is by definition 2
and thus true
Upvotes: 7
Reputation: 385174
In C++20, yes.
Here's how [expr.xor]
defines it:
Given the coefficients xi and yi of the base-2 representation ([basic.fundamental]) of the converted operands x and y, the coefficient ri of the base-2 representation of the result r is 1 if either (but not both) of xi and yi are 1, and 0 otherwise.
And [basic.fundamental]
covers what a base-2 representation means:
Each value x of an unsigned integer type with width N has a unique representation x = x020 + x121 + … + xN-12N-1, where each coefficient xi is either 0 or 1; this is called the base-2 representation of x. The base-2 representation of a value of signed integer type is the base-2 representation of the congruent value of the corresponding unsigned integer type.
In short, it doesn't really matter how it's done "physically": the operation must satisfy the more abstract, arithmetic notion of base-2 (whether this matches the bits in memory or not; of course in reality it will) and so XOR is entirely well-defined.
However, this was not always the case. The wording was introduced by P1236R1, to make it crystal clear how integer operations behave and to abstract away the kind of wooly notion of a "bit".
In C++11, all we knew is that signed integers must follow "A positional representation for integers that uses the binary digits 0 and 1, in which the values represented by successive bits are additive, begin with 1, and are multiplied by successive integral power of 2, except perhaps for the bit with the highest position" (footnote 49; be advised that this is non-normative).
This gets us most of the way there, actually, but the specific wording in [expr.xor]
wasn't there: all we knew is that "the result is the bitwise exclusive OR function of the operands". At this juncture, whether that refers to a sufficiently commonly understood operation is really up to you. You'll be hard-pressed to find a dissenting opinion on what this operation was permitted to do, mind you.
So:
In C++11, YMMV.
Upvotes: 11
Reputation: 182769
No matter how the values are represented internally, the result of 2 ^ 32
is 34
. The ^
operator means a binary XOR and the result you must get if you do that operation correctly is independent of how you do the operation.
The same is true of 2 + 32
. You can represent 2 and 32 in binary, in decimal, or any other way you want, but the result you get had better be the way you represent 34, whatever that is.
Upvotes: 1
Reputation: 308206
I don't know if the standard formally defines exclusive or, but it's a well known operation with a consistent definition. The one thing that is explicitly left out of the standard is the mapping of integer numbers to bits. Your assertion would hold for the commonly used twos-complement representation and the uncommon ones-complement.
Upvotes: -1