铸造类型是否依赖于大/小端?

时间:2022-06-16 16:09:44

Consider this little piece of code:

考虑一下这段代码:

int a=0x10000001;
char b;

b=(char)a;
printf("%#x \n",b);

On my PC it prints 0x01 and I am not suprised. How would it work on BIG ENDIAN machine? I expect that it would print 0x10000001. Am I right?

在我的电脑上,它打印0x01,我并不感到惊讶。它如何在BIG ENDIAN机器上运行?我希望它会打印0x10000001。我对吗?

I browsed books and web but I didn't find clear information how the casting operation really deals with the memory.

我浏览了书籍和网页,但我没有找到关于铸造操作如何真正处理内存的明确信息。

4 个解决方案

#1


15  

No, casting like the one in question does preserve value if possible and does not depend on memory representation.

不,如果可能,像所讨论的那样进行投射会保留值,并且不依赖于内存表示。

If you want to reinterpret the memory representation you need to cast pointers. Then it will depend on endianness:

如果要重新解释内存表示,则需要转换指针。然后它将取决于字节顺序:

b=*((char*)&a);

#2


8  

Numbers are not big- or little-endian. Sequences of bytes are big- or little-endian. Numbers are just numbers.

数字不是大端或小端。字节序列是大端或小端。数字只是数字。

C's numeric types deal, unsurprisingly, with numbers, not with sequences of bytes.

不出所料,C的数字类型处理的是数字,而不是字节序列。

#3


2  

No. Endianness doesn't matter in this example. Converting to char (assuming a char is narrower than an int) will keep the lower-order bits, and lower-order bits are lower-order bits, no matter how they are stored in memory.

在此示例中,Endianness无关紧要。转换为char(假设char比int窄)将保持低位,低位位是低位,无论它们如何存储在内存中。

#4


0  

C++ standard states the following:

C ++标准声明如下:

5.2.3.1 A simple-type-specifier (7.1.5) followed by a parenthesized expression-list constructs a value of the specified type given the expression list.

5.2.3.1简单类型说明符(7.1.5)后跟带括号的表达式列表,在给定表达式列表的情况下构造指定类型的值。

So yes, it constructs new instance demoting the wider type to shorter one regardless the binary representation. So if you're interested in exact part of the int to be passed to char, let use bitwise shift operator - it is platform-independent and produces predictable results.

所以是的,无论二进制表示如何,它都会构造新的实例,将较宽的类型降级为较短的类型。因此,如果您对要传递给char的int的确切部分感兴趣,请使用按位移位运算符 - 它与平台无关并产生可预测的结果。

#1


15  

No, casting like the one in question does preserve value if possible and does not depend on memory representation.

不,如果可能,像所讨论的那样进行投射会保留值,并且不依赖于内存表示。

If you want to reinterpret the memory representation you need to cast pointers. Then it will depend on endianness:

如果要重新解释内存表示,则需要转换指针。然后它将取决于字节顺序:

b=*((char*)&a);

#2


8  

Numbers are not big- or little-endian. Sequences of bytes are big- or little-endian. Numbers are just numbers.

数字不是大端或小端。字节序列是大端或小端。数字只是数字。

C's numeric types deal, unsurprisingly, with numbers, not with sequences of bytes.

不出所料,C的数字类型处理的是数字,而不是字节序列。

#3


2  

No. Endianness doesn't matter in this example. Converting to char (assuming a char is narrower than an int) will keep the lower-order bits, and lower-order bits are lower-order bits, no matter how they are stored in memory.

在此示例中,Endianness无关紧要。转换为char(假设char比int窄)将保持低位,低位位是低位,无论它们如何存储在内存中。

#4


0  

C++ standard states the following:

C ++标准声明如下:

5.2.3.1 A simple-type-specifier (7.1.5) followed by a parenthesized expression-list constructs a value of the specified type given the expression list.

5.2.3.1简单类型说明符(7.1.5)后跟带括号的表达式列表,在给定表达式列表的情况下构造指定类型的值。

So yes, it constructs new instance demoting the wider type to shorter one regardless the binary representation. So if you're interested in exact part of the int to be passed to char, let use bitwise shift operator - it is platform-independent and produces predictable results.

所以是的,无论二进制表示如何,它都会构造新的实例,将较宽的类型降级为较短的类型。因此,如果您对要传递给char的int的确切部分感兴趣,请使用按位移位运算符 - 它与平台无关并产生可预测的结果。