Why not enforce 2's complement in C++?

后端 未结 4 923
囚心锁ツ
囚心锁ツ 2020-12-09 15:36

The new C++ standard still refuses to specify the binary representation of integer types. Is this because there are real-world implementations of C++ that don\'t use 2\'s co

4条回答
  •  庸人自扰
    2020-12-09 16:01

    (Edit: C++20 now imposes 2's complement representation, note that overflow of signed arithmetic is still undefined and shifts continue to have undefined and implementation defined behaviors in some cases.)

    • A major problem in defining something which isn't, is that compilers were built assuming that is undefined. Changing the standard won't change the compilers and reviewing those to find out where the assumption was made is a difficult task.

    • Even on 2 complement machine, you may have more variety than you think. Two examples: some don't have a sign preserving right shift, just a right shift which introduce zeros; a common feature in DSP is saturating arithmetic, there assigning an out of range value will clip it at the maximum, not just drop the high order bits.

提交回复
热议问题