C++ 字节大小(说明)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6149740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Size of byte (clarification)
提问by Guus Geurkink
I'm writing a game server, and this might be an easy question, but I just want some clarification.
我正在编写一个游戏服务器,这可能是一个简单的问题,但我只想澄清一下。
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?
为什么一个字节(字符或无符号字符)最多可以容纳 255(0xFF,我认为是 2 个字节)的值?当我使用sizeof(unsigned char)
编译器时,它告诉我它是 1 个字节。是因为(在 ACSII 中)它正在“转换”为角色吗?
Sorry for this poor explaination, I'm not really good at describing a question.
对不起,这个糟糕的解释,我不太擅长描述一个问题。
回答by Yann Vernier
This touches on a bunch of subjects, including the historical meaning of a byte, the C definition of a char, and mathematics.
这涉及很多主题,包括字节的历史意义、字符的 C 定义和数学。
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 位。作为文字游戏,还有半字节(不称为咬)的 nybble(或通常是 nibble)。
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 complementis the most popular; in that case, we get one more negative value than positive, for a range of -128..+127
.
数学告诉我们,通过 8 个1-or-0
值的有序组合,我们得到了组合。有时我们使用无符号,有时使用有符号,但无论哪种方式,我们都希望范围内为 0;所以无符号范围是. 对于有符号范围,我们有更多的选择,其中以二进制补码最为流行;在这种情况下,对于 的范围,我们得到的负值比正值多一个。28= 256
0..255
-128..+127
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 least8 bits, or one byte; exactly one byte if the machine supports it.
C++ 从 C 继承 char,其中它被定义为具有sizeof
1 的 a,是最小的可寻址大小(即具有不同的地址值和 &),以及最小范围-128..127
或0..255
取决于它是否有符号。这归结为至少需要8 位或一个字节;如果机器支持,正好是一个字节。
0xff
is another way of writing 255. 0x
is the C way of marking a hexadecimalconstant, 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.
0xff
是另一种写法 255.0x
是标记十六进制常量的 C 方式,因此其中的每个数字都是 4 位(对于 16 个可能的数字),因此是半字节。这将转换为所有位都设置为 1 的无符号八位字节。
If specific size matters to your code, there is a header stdint.hthat defines types of minimal and exact sizes, for speed or size optimization.
如果特定大小对您的代码很重要,则有一个标头stdint.h定义了最小和精确大小的类型,用于速度或大小优化。
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.
顺便提一下,ASCII 是一个 7 位字符集。如今,具有 7 位字节的机器并不常见,而更广泛的字符集(如 ISO 8859-1 和 UTF-8)也很流行。
回答by Bj?rn Pollex
0xFF
can be stored in 8 bits, which is one byte.
0xFF
可以存储为 8 位,也就是 1 个字节。
sizeof(char)
is defined to always return 1, regardless of the actual size in bits of the underlying datatype (see 5.3.3.1 of the current standard). The sizes of all other dataypes are calculated relative to the size of a char
.
sizeof(char)
被定义为总是返回 1,不管底层数据类型的实际大小(见当前标准的 5.3.3.1)。所有其他数据类型的大小是相对于 a 的大小计算的char
。
回答by Prasoon Saurav
When I use sizeof(unsigned char) the compiler tells me it is 1 byte.
当我使用 sizeof(unsigned char) 时,编译器告诉我它是 1 个字节。
The size of char
[whether it is signed or unsigned ] is always 1 as mandated by the C++ Standard.
的大小char
[无论是有符号或无符号]始终为1的授权由C ++标准。
回答by Alok Save
Sizeof char or unsigned char is 1 Byte as per the standard.
Sizeof char 或 unsigned char 根据标准为 1 字节。
Why different ranges if same size?
如果尺寸相同,为什么不同的范围?
1 Byte = 8 bits or 2^8
2^8 = 256
Hence,signed char
range is from -128 to 127
unsigned char
range is from 0 to 255
因此,signed char
范围是从-128 to 127
unsigned char
范围是从0 to 255
This is because in case of signed char
one of the bits is used to store the sign, while since unsigned char
cannot be -ve, that bit is utlized to increase the range.
这是因为如果signed char
其中一个位用于存储符号,而由于unsigned char
不能是 -ve,则该位用于增加范围。
回答by Yankes
char
size is always 1
but number of bits can differ, C define macro CHAR_BIT
that have number of bits in char.
This mean maximum value that unsigned char
can have is pow(2, CHAR_BIT) - 1
.
char
size 总是1
但位数可以不同,C 定义CHAR_BIT
了在 char 中有位数的宏。这意味着unsigned char
可以具有的最大值是pow(2, CHAR_BIT) - 1
。
More info there: What is CHAR_BIT?
更多信息:什么是CHAR_BIT?
回答by Ahmed Kotb
1 byte is 8 bits so in case of
1 字节是 8 位,所以在
- signed : (1 bit is used for sign so 2^7 = 128) it holds from -128 to 127
- unsigned : (2^8 = 255) it holds from 0 to 255
- 有符号:(1 位用于符号,所以 2^7 = 128)它从 -128 到 127
- 无符号:(2^8 = 255) 它从 0 到 255
回答by geofftnz
255, 0xFF is one byte when represented as an unsigned char. You cannot represent 255 as a signed char.
255,当表示为无符号字符时,0xFF 是一个字节。您不能将 255 表示为有符号字符。