c - 在整数常量上移动会显示警告。如何清除这个?
问题描述
参考:整数常量中的后缀
unsigned long long y = 1 << 33;
结果警告:
left shift count >= width of type [-Wshift-count-overflow]
需要从上述上下文中清除两个问题:
- unsigned long long 类型有 64 位,为什么我们不能在其中进行左移?
- int 常量('1')中的移位如何工作?
解决方案
在 C 语言中,1 是int
大多数平台上的 32 位。当您尝试在将其值存储到中之前unsigned long long
将其移动 33 位时,这不会很好地结束。您可以通过 2 种方式解决此问题:
- 改为使用
1ULL
,这是一个unsigned long long
常数:
unsigned long long y = 1ULL << 33;
- 赋值,然后移位:
unsigned long long y = 1;
y <<= 33;
两者都是有效的,但我建议第一个,因为它更短,你可以制作y
const。
推荐阅读
- javascript - ReactJs 中父项下出现多个字段
- xamarin.forms - 无法将 PopupPage 添加到 Visual Studio 中的项目
- python - 如何在 Python 中格式化 txt 文件
- apiconnect - 在 IBM Api connect 中,无法将响应 Content-Type 更改为“application/json; 字符集=UTF-8”
- reactjs - 当单击侧边栏项目时,想要传递一些道具来检查隐藏或显示组件中的某些按钮,核心 ui 中的侧边栏菜单 reactjs
- java - @PreAuthorize 注解无法解析变量
- android - UnmetDependencyException:缺少 Crashlytics 构建 ID
- c++ - 预期的 ';' 在 qt 中的声明结束时
- python - 如何将数据输入我的神经网络?
- arrays - 为什么我得到 TypeError: _this.data.forEach is not a function