字节大小(澄清) [英] Size of byte (clarification)

查看:168
本文介绍了字节大小(澄清)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个游戏服务器,这可能是一个容易的问题,但我只是想要一些澄清。

I'm writing a game server, and this might be an easy question, but I just want some clarification.

为什么一个字节(char或unsigned char)可以容纳255的值(0xFF,我相信是2个字节)?当我使用 sizeof(unsigned char)时,编译器告诉我它是1个字节。
是因为(在ACSII中)它被转换为一个字符?

Why is it that a byte (char or unsigned char) can hold up to a value of 255 (0xFF, which I believe is 2 bytes)? When I use sizeof(unsigned char) the compiler tells me it is 1 byte. Is it because (in ACSII) it is getting "converted" to a character?

对于这个不好的解释,我不是很善于描述一个问题。

Sorry for this poor explaination, I'm not really good at describing a question.

推荐答案

这涉及到一系列主题,包括字节的历史意义, ,数学。

This touches on a bunch of subjects, including the historical meaning of a byte, the C definition of a char, and mathematics.

对于初学者来说,一个字节历来是很多东西,但是现在我们几乎总是意味着一个8位字节。作为一个字的游戏,还有一个半字节(不叫咬)的nybble(或经常半字节)。

For starters, a byte has historically been a lot of things, but nowadays we nearly always mean an octet, which is 8 bits. As a play on words, there's also the nybble (or often nibble) which is half a byte (not called bite).

数学告诉我们,使用8 1或0 值的有序组合, $ c> 2 8 = 256 组合。有时我们使用这个无符号,有时是有符号的,但无论哪种方式,我们想在范围内有0;因此无符号范围为 0..255 。对于已签名的范围,我们有更多选项,其中二进制补码是最受欢迎的;在这种情况下,对于范围 -128 .. + 127 ,我们得到比正值更多的负值。

Mathematics tells us that with an ordered combination of 8 1-or-0 values, we get 28 = 256 combinations. Sometimes we use this unsigned, sometimes signed, but either way we want to have 0 in the range; so the unsigned range is 0..255. For the signed range, we have more options, of which two's complement is the most popular; in that case, we get one more negative value than positive, for a range of -128..+127.

C ++从C继承char,其中定义为具有 sizeof 为1的最小可寻址大小(即具有&)的不同地址值)取决于是否已签名,最小范围为 -128..127 0..255 这归结为需要至少 8位,或一个字节;正好一个字节,如果机器支持它。

C++ inherits char from C, where it is defined to have a sizeof of 1, to be the smallest addressable size (i.e. having distinct address values with &), and a minimal range of -128..127 or 0..255 depending on if it's signed or not. That boils down to requiring at least 8 bits, or one byte; exactly one byte if the machine supports it.

0xff 是另一种写入方式255。 0x 是标记十六进制常数的C方式,因此其中的每个数字为4位(对于16个可能的数字),ergo啃。这转换为一个无符号的八位字节,所有位都设置为1。

0xff is another way of writing 255. 0x is the C way of marking a hexadecimal constant, so each digit in it is 4 bits (for 16 possible digits), ergo the nibble. This translates to an unsigned octet with all bits set to 1.

如果特定大小对您的代码很重要,则有一个头 stdint.h ,它定义了最小和精确大小的类型,用于速度或大小优化。

If specific size matters to your code, there is a header stdint.h that defines types of minimal and exact sizes, for speed or size optimization.

顺便说一句,ASCII是一个7位字符集。具有7位字节的机器现在是不寻常的,更宽的字符集,如ISO 8859-1和UTF-8是流行的。

Incidentally, ASCII is a 7-bit character set. Machines with 7-bit bytes are unusual nowadays, and wider character sets like ISO 8859-1 and UTF-8 are popular.

这篇关于字节大小(澄清)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆