移位时是否有 n 位的限制?
Is there a limit of n bits when bit shifting?
本文关键字:是否 更新时间:2023-10-16
在尝试为位板类提出方案时,我决定使用全局编译时变量来表示关键位板配置,例如所有黑鸦的初始位置。
constexpr uint64_t BLACK_ROOK_INIT = 0x1 | (0x1 << 56);
但是我收到编译器错误。 编译器似乎将此值视为 32 位值,类型转换或添加额外的 0 似乎没有区别。 类型定义来自 。
一旦我从这个表达式中删除 constexp,它就会编译,但仍然会产生等效的警告。为什么会这样?我认为这可能是预处理器的限制,但是在没有constexp的情况下,问题仍然存在。
chess.cpp:16:64: error: right operand of shift expression ‘(1 << 56)’ is >= than the precision of the left operand [-fpermissive]
仅供参考,这也不会编译
constexpr int64_t BLACK_ROOK_INIT = (int64_t)0x1 | (int64_t)(0x1 << 32);
这是你想要的:
#include <iostream>
int main(){
constexpr uint64_t BLACK_ROOK_INIT = 0x1ULL | (0x1ULL << 56);
std::cout<<BLACK_ROOK_INIT<<std::endl;
}
默认情况下,您的0x1
值是一个int
,通常实现为 32 位整数。
此处讨论后缀。如果他们让你有点不舒服,就像他们让我一样,你可以这样投射:
#include <iostream>
int main(){
constexpr uint64_t BLACK_ROOK_INIT = (uint64_t)(0x1) | ((uint64_t)(0x1) << 56);
std::cout<<BLACK_ROOK_INIT<<std::endl;
}
这里的 1 是 int,移动后它超过了 int 的极限。 所以首先我们需要将 int 转换为长 long int(根据您的要求进行 A/C(
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
ll l=(long long int)1<<n;
cout<<l<<endl;
}
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 检查输入是否不是整数或数字
- 是否可以初始化不可复制类型的成员变量(或基类)
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 此代码是否违反一个定义规则
- 是否需要删除包含对象的"pair"?
- 是否可以从int转换为enum类类型
- 无论条件是否为true,if总是在c++中执行
- 如何找到大小'x'数组是否完全填充,在C++?
- 检查值是否在集合p1和p2中,但不在p3中
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- 检查 std::shared_ptr<> 的当前底层类型是否为 T
- 在c++中检查长方体是否尽可能快地重叠(无迭代)
- GL_SHADERSTORAGE_BUFFER位置是否与其他着色器位置冲突
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- C/C++预处理器是否可以检测一些编译器选项
- 是否可以用"iostream"包装现有的TCP/OOpenSSL会话