知识卡片
整数类型声明可表示域
有符号 N 位整数用二补码表示 −2 的 N−1 次幂到 2 的 N−1 次幂减一。无符号 N 位从零到 2 的 N 次幂减一。例中 u8 最大 255,i8 最大 127 且可到 −128。usize 的位宽随目标变化,常给集合索引用。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 常见编程概念 Data Types · 查看原书依据
Each signed variant can store numbers from –(2n – 1) to 2n – 1 – 1 inclusive, where n is the number of bits that variant uses. So an i8 can store numbers from –(27) to 27 – 1, which equals –128 to 127.
参考来源
- 位置:《The Rust Programming Language(第2版)》第03章 常见编程概念(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c03.xhtml`,块 0069)
- 结论依据:原文以 i8 与 u8 说明相同位宽时 signedness 改变取值范围。
- 原始内容:Each signed variant can store numbers from –(2n – 1) to 2n – 1 – 1 inclusive, where n is the number of bits that variant uses. So an i8 can store numbers from –(27) to 27 – 1, which equals –128 to 127.