关于数字表示

发布于 2024-11-01 03:05:17 字数 24 浏览 0 评论 0原文

如何找到我所在系统的数字表示形式?

How do I find the representation of a Number for the system I am on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

奈何桥上唱咆哮 2024-11-08 03:05:17

AFAIK 每台现代计算机在表示数字时都使用二进制。已经用其他类型的计算机进行了实验,但它们未能与二进制竞争。

然而,他们正在使用量子计算机,其工作方式完全不同,并以完全不同的方式表示数字。

AFAIK every modern computer uses binary when representing a number. Experiments have been made with other kind of computers, but they have failed to compete with binary.

However, they are working a quantum computers which work completely different and represent numbers on a completely different way.

画骨成沙 2024-11-08 03:05:17

通常的方法是将数字存储在内存中,然后检查内存。

volatile number_type x;
x = 512.123;

typedef unsigned char const volatile uccv;
uccv *c = reinterpret_cast< uccv * >( & x );
std::cout << std::hex;
std::cout.fill( '0' );
for ( uccv *pen = c; pen != c + sizeof x; ++ pen ) {
    std::cout.width( 2 );
    std::cout << static_cast< unsigned >( * pen );
}
std::cout << std::dec << '\n';

对于易失性表示歉意;我不记得严格的别名规则,并且现在不想查找它们。

The usual way is to store the number in memory, and then inspect the memory.

volatile number_type x;
x = 512.123;

typedef unsigned char const volatile uccv;
uccv *c = reinterpret_cast< uccv * >( & x );
std::cout << std::hex;
std::cout.fill( '0' );
for ( uccv *pen = c; pen != c + sizeof x; ++ pen ) {
    std::cout.width( 2 );
    std::cout << static_cast< unsigned >( * pen );
}
std::cout << std::dec << '\n';

Apologies for the volatile; I do not recall strict aliasing rules and don't want to look them up right now.

伴随着你 2024-11-08 03:05:17

了解符号表示实际上很简单。查看结果

(favoriteType)-1 & (favoriteType)3 

一旦您了解了 C 允许的三种不同的符号表示如何工作(参见例如 Acme 的答案),您将轻松计算出它们的此类表达式的值。

To know the sign representation is actually quite simle. Look at the result of

(favoriteType)-1 & (favoriteType)3 

Once you have understood how the three different sign representations work that C allows (see eg. Acme's answer), you will easily work out the values of such an expression for them.

一江春梦 2024-11-08 03:05:17

诸如表示(字大小、二进制补码与符号大小)和字节序等架构问题最好通过硬件和/或操作系统和/或编译器文档来回答。

您可以使用类型双关来检查值的各个字节:

T value = ...; // for some numeric type T (int, short, long, double, float, etc.)
unsigned char *p = (unsigned char*) &value;
size_t i;

printf("%10s%8s\n", "address", "value");
printf("%10s%8s\n", "-------", "-----");
for (i = 0; i < sizeof value; i++)
  printf("%10p%8x\n", p+i, (unsigned int) p[i]);

不过,对于大端与小端,您可以执行

unsigned int value = 0x01;
unsigned char *p = (unsigned char *) &value;
if (p[0] == 1)
  printf("Little-endian\n");
else if (p[sizeof value - 1] == 1)
  printf("Big-endian\n");
else
  printf("Weird\n");

“Better to RTM”之类的操作。

Architectural questions such as representation (word size, two's- vs. one's-complement vs. sign-magnitude) and endianness are best answered with hardware and/or OS and/or compiler documentation.

You can use type punning to examine the individual bytes of a value:

T value = ...; // for some numeric type T (int, short, long, double, float, etc.)
unsigned char *p = (unsigned char*) &value;
size_t i;

printf("%10s%8s\n", "address", "value");
printf("%10s%8s\n", "-------", "-----");
for (i = 0; i < sizeof value; i++)
  printf("%10p%8x\n", p+i, (unsigned int) p[i]);

For big- vs. little-endian, you could do something like

unsigned int value = 0x01;
unsigned char *p = (unsigned char *) &value;
if (p[0] == 1)
  printf("Little-endian\n");
else if (p[sizeof value - 1] == 1)
  printf("Big-endian\n");
else
  printf("Weird\n");

Better to RTM, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文