对于整数值,小尾数表示法和大尾数表示法的区别非常简单。

但是,对于我来说,尚不清楚小端浮点数与大端浮点数有何不同。

最后,我想知道哪个更常用。

最佳答案

一些消息来源说,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;
}

它是在小印度机器上输出的:

浮点数:3.141590
字节数组:D0:F:49:40
float :-3.141590
字节数组:D0:F:49:C0

从理论上讲,在big-endian机器上,字节的顺序将被颠倒。

引用:
http://betterexplained.com/articles/understanding-big-and-little-endian-byte-order/

关于c++ - 大端浮子与小端浮子有什么区别?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9056373/

10-13 06:02