字节(Byte)与位(Bit)

什么是字节和位?

计算机存储单位关系:
1 字节(Byte,B)= 8 位(bit)

单位(Unit)换算关系(Conversion)
1 bit(位)只能存 0 或 1(Stores 0 or 1)
1 Byte (B)(字节)8 bit
1 KB (Kilobyte)(千字节)1024 B
1 MB (Megabyte)(兆字节)1024 KB
1 GB (Gigabyte)(吉字节)1024 MB
1 TB (Terabyte)(太字节)1024 GB

2. 一个字节能存多少数据?(How much data can a byte store?)

一个字节(8 位)能表示 2^8 = 256 种可能值
(A single byte (8 bits) can represent 2^8 = 256 possible values.)

  • 无符号整数(Unsigned integer)0 ~ 255
  • 有符号整数(Signed integer, two’s complement)-128 ~ 127
  • 字符(Character in ASCII encoding)
    • 'A'(65)→ 01000001
    • 'B'(66)→ 01000010

如果是多字节存储,能表示更大范围: (If stored using multiple bytes, a larger range can be represented.)

数据类型(Type)占用字节数(Bytes)存储范围(无符号 Unsigned)存储范围(有符号 Signed)
char1 B0 ~ 255-128 ~ 127
short2 B0 ~ 65,535-32,768 ~ 32,767
int4 B0 ~ 4,294,967,295-2,147,483,648 ~ 2,147,483,647
long long8 B0 ~ 18,446,744,073,709,551,615-9,223,372,036,854,775,808 ~
9,223,372,036,854,775,807

3. 不同数据存储占用的字节

(Different Data Storage in Bytes)

(1)整数存储(Integer storage)

c复制编辑int a = 10;   // 4 字节(32 位)(4 bytes, 32 bits)
short b = 20; // 2 字节(16 位)(2 bytes, 16 bits)

(2)字符存储(Character storage)

c复制编辑char c = 'A';   // 1 字节(存储 ASCII 码 65)(1 byte, stores ASCII code 65)
char str[] = "Hello"; // 6 字节(5 个字符 + 1 个 '\0' 结束符)(6 bytes: 5 characters + 1 null terminator)

(3)浮点数存储(Floating-point storage)

c复制编辑float f = 3.14;  // 4 字节(IEEE 754 格式)(4 bytes, IEEE 754 format)
double d = 3.14; // 8 字节 (8 bytes)

4. 计算机如何存储数据(How does a computer store data?)

计算机存储数据时,使用 二进制(0 和 1) 来表示。
(Computers store data using binary (0s and 1s).)

例如: (For example:)

数据(Data)二进制表示(Binary Representation)占用字节(Bytes Used)
'A'(ASCII 码 65010000011 B
10(整型 1000000000 00000000 00000000 000010104 B
3.14(浮点型 IEEE 75401000000 01001000 11111011 011111104 B

5. 不同编码下的字符存储(Character storage in different encodings)

(Different encodings affect character storage size.)

字符(Character)ASCII(1字节)UTF-8(可变长 1-4字节)UTF-16(2或4字节)GBK(2字节,中文专用)
'A'1 B1 B2 B1 B
'中'❌(不支持)3 B2 B2 B
'😃'❌(不支持)4 B4 B❌(不支持)

6. 结论(Conclusion)

1 字节 = 8 位 (1 byte = 8 bits)
存储数据的大小取决于数据类型(如 int 4B, char 1B)(The size of stored data depends on its type.)
字符、数字、浮点数在存储时有不同的编码方式(ASCII、补码、IEEE 754)(Characters, numbers, and floating-point values have different encoding methods.)

THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容