1

Seeing that the answers to these questions (this and this one) talk about C, I cannot extend this knowledge to C++, so this is not a duplicate, but a clarification.

Update: I'm only talking about C++. If something is defined in C, it does not mean that it is defined in C++ and vice versa, which is why I am asking this question.

0dminnimda
  • 1,242
  • 3
  • 12
  • 29
  • 1
    The other question is tagged C++ and C. It's not our fault there's no satisfying C++ answer there. The question **does feel like a duplicate**. C11 answers are coming just like before. – Wyck Aug 05 '21 at 21:09
  • Does this answer your question? [Can bitwise operators have undefined behavior?](https://stackoverflow.com/questions/46387704/can-bitwise-operators-have-undefined-behavior) – Wyck Aug 05 '21 at 21:10
  • 1
    What version of C++ are you asking about? Pretty sure it's well defined behavior in C++20 as signed integers are now guaranteed to be twos-compliment. Before that. most likely implementation defined behavior. – NathanOliver Aug 05 '21 at 21:11
  • @Wyck No, I even listed this link as the first example in my question ... – 0dminnimda Aug 05 '21 at 21:11
  • @NathanOliver, I will be glad to know about any version, if it is defined in any. – 0dminnimda Aug 05 '21 at 21:16
  • 1
    Well this is the text from C++20 (acutally the C++23 draft now), and it doesn't appear to me to leave any leeway into what the value could be: https://timsong-cpp.github.io/cppwp/expr.xor – NathanOliver Aug 05 '21 at 21:17
  • 3
    I removed the C++ tag from the other questions (they should not be dual tagged, and did not have any C++ answers) – M.M Aug 05 '21 at 21:22
  • 1
    Those operators are defined on bits. In theory, the only thing implementation defined would be the actual integer value of negative numbers before C++ 20. – Phil1970 Aug 05 '21 at 21:51

1 Answers1

1

According to Bitwise logic, xor can not have UB or IB in results of integer types.

The xor operator performs usual arithmetic conversions on both operands of other types that may lead undefined behavior.

273K
  • 29,503
  • 10
  • 41
  • 64
  • Sorry for being meticulous, but are there any trap values for the one's complement system that don't support negative zeros in signed integers (as pointed out for C in [this answer](https://stackoverflow.com/a/46387882))? – 0dminnimda Aug 05 '21 at 21:49
  • 2
    @0dminnimda note that _"...However, all C++ compilers use two's complement representation, and as of C++20, it is the only representation allowed by the standard, ..."_ https://en.cppreference.com/w/cpp/language/types . Also _"...8-bit one's complement and sign-and-magnitude representations for char have been disallowed since C++11 ..."_ same link – Richard Critten Aug 05 '21 at 22:05