does in c++ the conversion from unsigned int to int always preserve the bit pattern?

前端 未结 3 1147
我在风中等你
我在风中等你 2021-02-13 15:37

From the standard (4.7) it looks like the conversion from int to unsigned int, when they both use the same number of bits, is purely conceptual:

If the de

3条回答
  •  佛祖请我去吃肉
    2021-02-13 16:30

    You cannot assume anything.

    The first quote doesn't state that the bitmask remains the same. It may be the same in two's complement, but not in one's complement or other representations.

    Second, implementation-defined means implementation-defined, you can't assume anything in general.

    In theory, the representation can be completely different after each conversion. That's it.


    If you look at it in a realistic way things come more concrete. Usually, int's are stored in two's complement and signed->unsigned preserves the pattern as unsigned->signed does (since the value can be implementation-defined, the cheapest way is doing nothing).

提交回复
热议问题