Does cast between signed and unsigned int maintain exact bit pattern of variable in memory?

前端 未结 3 859
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-13 04:41

I want to pass a 32-bit signed integer x through a socket. In order that the receiver knows which byte order to expect, I am calling htonl(x) befor

3条回答
  •  情歌与酒
    2020-12-13 04:59

    This should always be safe, because the intXX_t types are guaranteed to be in two's complement if they exist:

    7.20.1.1 Exact-width integer types The typedef name intN_t designates a signed integer type with width N , no padding bits, and a two’s complement representation. Thus, int8_t denotes such a signed integer type with a width of exactly 8 bits.

    Theoretically, the back-conversion from uint32_t to int32_t is implementation defined, as for all unsigned to signed conversions. But I can't much imagine that a platform would do differently than what you expect.

    If you want to be really sure of this you still could to that conversion manually. You'd just have to test a value for > INT32_MAX and then do a little bit of math. Even if you do that systematically, a decent compiler should be able to detect that and optimize it out.

提交回复
热议问题