英文:
Strange error when bit shifting uint64_t by a uint16_t in cpp
问题
下面的函数试图通过将0x1左移N次来创建一个位板,其中设置的位位于第N个位置。N由uint16_t中的第1到6个最低有效位给出。然后通过掩码来隔离6个最低有效位。
```c
uint64_t endSquareFinder(uint16_t a){
a &= 0x003f;
return 0x0000000000000001 << a;
}
所有输入都有效,除了当a = 0x001f时,函数的输出为0xffffffff80000000,而不是0x0000000080000000。这对我来说非常奇怪。
<details>
<summary>英文:</summary>
The function below is attempting to create a bitboard with the set bit being in the Nth position, by bitshifting 0x1 N times to achieve desired result. N is being given by the 1-6th least significant bits in a uint16_t. It is then masked to isolate 6 lsbs.
uint64_t endSquareFinder(uint16_t a){
a &= (0x003f);
return 0x0000000000000001 << a;
}
All inputs work except for when a = 0x001f, the output of the function is 0xffffffff80000000 rather than 0x0000000080000000. This to me is very bizare.
gdb compiler
</details>
# 答案1
**得分**: 6
需要将`1`转换为一个无符号64位整数。现在它是一个`int`...
```c++
#include <type_traits>
// 这个通过了:
static_assert(std::is_same_v<decltype(0x0000000000000001), int>);
... 这很可能只有32位。
示例:
return std::uint_least64_t(1) << a;
// 或者
return 1ull << a; // "unsigned long long int" 至少有64位
英文:
You need to make 1
into an unsigned 64 bit integer. Right now it's an int
...
#include <type_traits>
// this passes:
static_assert(std::is_same_v<decltype(0x0000000000000001), int>);
... which is most likely only 32 bits.
Example:
return std::uint_least64_t(1) << a;
// or
return 1ull << a; // "unsigned long long int" is at least 64 bits
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论