Can a C compiler change bit representation when casting signed to unsigned?

前端 未结 3 1288
借酒劲吻你
借酒劲吻你 2021-02-03 13:07

Is it possible for an explicit cast of, say, int32_t to uint32_t, to alter the bit representation of the value?

For example, given that I have

3条回答
  •  自闭症患者
    2021-02-03 14:07

    If the value is in the range of both the signed and the unsigned types, then both the value and representation are unchanged by conversions.

    Otherwise, the signed-to-unsigned conversion is only allowed to preserve the bit representation when the implementation's representation of negative values for the type is twos-complement. For ones complement or sign-magnitude, it conversion must change the representation. The conversion in the other direction is implementation-defined, so it may or may not change the representation.

提交回复
热议问题