8位表示2个数字

发布于 2024-12-18 06:52:32 字数 170 浏览 2 评论 0原文

我有两组价值观。每个值的范围都是 -50 到 + 50。

有没有办法用一个字节来表示其中两个值? (我正在使用 C 语言工作,使用 Vstudio 2010)。 谢谢。

澄清:这些值是任意整数;也就是说,这些值可以是 -50 到 +50 之间的任何整数。 (所以,问题已经得到解答:“不”。)

I have 2 sets of values. Each is in the range of -50 to + 50.

Is there any way to represent two of these values in a single byte?
(I am working in C, using Vstudio 2010).
Thank you.

Clarification: the values are arbitrary integers; that is, the values can be
any integer between -50 and +50. (So, question has been answered: it is "no".)

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

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

发布评论

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

评论(4

不必你懂 2024-12-25 06:52:32

不,不是 8 位。 -50 到 +50 是 101 种可能性。两个人就有10201种可能。 8位只有256种组合。

您至少需要 14 位来存储 -50 到 +50 两个值。

No, not in 8 bits. -50 to +50 is 101 possibilities. With two of them, that's 10201 possibilities. 8 bits only has 256 combinations.

You will need a minimum of 14 bits to store two values -50 to +50.

赏烟花じ飞满天 2024-12-25 06:52:32

这取决于。这取决于几个因素:

  • -50 到 +50 范围内的值是什么类型的值
  • 一个字节中有多少位

如果 -50 到 +50 范围内的值是任意浮点数或整数,您可能不会能够在一个字节中容纳其中的 2 个。是否属于这种情况取决于字节中的位数。在 Visual C++ 中,字节是 8 位的。正如其他人所解释的那样,8 位对于 -50 到 +50 之间的任意整数来说太少了。在某些平台上,字节可以大到 16 位。在这种情况下,显然,您可以将 -50 到 +50 之间的几个整数打包到 16 位中。 AFAIK,在 C/C++ 中不能使用小至 16 位或更小的浮点数。

现在,如果从 -50 到 +50 的值不是任意浮点或整数值,如果这些值只能是 -50 或 +50,则 1 位对于一个这样的值就足够了。如果它们只能是 -50、0 或 +50,则 2 位就足以满足一个这样的值。如果它们只能是 -50、-25、0、+25 或 +50,则 3 位就足以满足一个这样的值。

关键是,答案很大程度上取决于问题的提出方式。您的问题没有明确地将 -50 到 +50 范围内的值定义为任意整数或任意浮点数,也不暗示这种值。除非您的问题得到澄清,否则没有一个明确的答案,例如,您能得到的最好答案是也许

It depends. It depends on several things:

  • what kind of values those values in the range of -50 to +50 are
  • how many bits there are in a byte

If those values from -50 to +50 are arbitrary floating point or integer numbers, you may not be able to fit 2 of them in a single byte. Whether or not that is the case depends on the number of bits in a byte. In Visual C++ bytes are 8-bit. 8 bits are too few for arbitrary integers from -50 to +50 as it has been explained by other people. On some platforms bytes can be as large as 16 bits. In that case, obviously, you can pack several integers from -50 to +50 into 16 bits. AFAIK, you can't have floating point numbers as small as 16 bits or smaller in C/C++.

Now, if those values from -50 to +50 are not arbitrary floating point or integer values, if those values can only be either -50 or +50, 1 bit is enough for one such value. If they can only be -50, 0 or +50, 2 bits are enough for one such value. If they can only be -50, -25, 0, +25 or +50, 3 bits are enough for one such value.

The point is, the answer largely depends on how the question is formulated. Your question does not explicitly define the values in the range from -50 to +50 as arbitrary integers or arbitrary floating point numbers, nor does it imply that kind of values. Unless your question is clarified, there's no single definitive answer such as yes or no, the best you can get is maybe.

阳光的暖冬 2024-12-25 06:52:32

-50 到 50 表示 100 的范围。您需要列表 7 位来表示 100 的范围(7 位,128 个值,6 位,64 个值)。因此,对于 8 位,无法保持完全精度并且有 2 个数字。如果你可以接受失去精度,那么,可以这样做。

-50 to 50 means a range of 100. You need at list 7 bits to represent a range of 100 (7 bits, 128 values, 6 bits, 64 values). So with 8 bits, can't keep full precision and have 2 numbers. If you are ok with loosing precision, then, can do.

笙痞 2024-12-25 06:52:32

简短的回答是否定的。

长答案是,在大多数架构上,一个字节是 8 位。一半是 4 位。 4 位只能表示 -8 到 7,使用二进制补码,这是大多数体系结构中表示整数的主要方式。

Short Answer is no.

Long answer is, that on most architectures, a byte is 8 bits. Half that is 4 bits. 4 bits can only represent -8 to 7, using twos complement, which is the dominant means of representing ints in most architectures..

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