And*_*uel 8 c c++ floating-point endianness
对于整数值,小端和大端表示的差异非常简单.
但我不清楚一个小端浮子与一个大端浮子有什么不同.
最后,我想知道哪个更常用.
一些消息来源称IEEE754浮点数总是以小端存储,但浮点数的IEEE754规范根本不包括字节序问题,并且可能因机器而异.以下是浮点/字节数组转换的示例代码:
#include <stdio.h>
int main(int argc, char** argv){
char *a;
float f = 3.14159; // number to start with
a = (char *)&f; // point a to f's location
// print float & byte array as hex
printf("float: %f\n", f);
printf("byte array: %hhX:%hhX:%hhX:%hhX\n", \
a[0], a[1], a[2], a[3]);
// toggle the sign of f -- using the byte array
a[3] = ((unsigned int)a[3]) ^ 128;
//print the numbers again
printf("float: %f\n", f);
printf("byte array: %hhX:%hhX:%hhX:%hhX\n", \
a[0], a[1], a[2], a[3]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它是在印度小机器上的输出:
float:3.141590 byte array:D0:F:49:40 float:-3.141590 byte array:D0:F:49:C0
从理论上讲,在大端机器上,字节的顺序是相反的.
参考:http: //betterexplained.com/articles/understanding-big-and-little-endian-byte-order/
| 归档时间: |
|
| 查看次数: |
8277 次 |
| 最近记录: |