知识卡片
移位检查针对距离
处理移位溢出时,检查的是移多少位。对 u64 来说,65 位超出 0 到 63 的有效范围。10_u64.checked_shr(65) 因此返回 None;10_u64.wrapping_shl(65) 把距离按 64 取模为 1,得到 20。用这些方法判断距离的合法性时,还需独立考虑移位是否会丢失结果中的高位。
关联阅读:[[显式选择整数溢出策略]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Fundamental Types 整数溢出时界定结果 · 查看原书依据
pattern. For these operations it is the shift distance, not the result, that can be out of range (again causing undefined behavior in C++).
参考来源
- 位置:《Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)》第02章《Fundamental Types》|整数溢出时界定结果,来源项目 `book-source`,`extracted/Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)/_epub-src/OEBPS/ch02.xhtml#handling-integer-overflow`
- 结论依据:原文区分移位距离与数值结果,并用同一个超范围距离展示 checked 与 wrapping 的不同处理。
- 原始内容:pattern. For these operations it is the shift distance, not the result, that can be out of range (again causing undefined behavior in C++).