左移 0 有什么意义
What is the point of left shifting by 0?
我目前正在尝试为开源服务器实现谷歌身份验证器,他们有这么一小段代码
if (securityFlags & 0x01) // PIN input
{
pkt << uint32(0);
pkt << uint64(0) << uint64(0); // 16 bytes hash?
// This triggers the 2 factor authenticator entry to popup on the client side
}
if (securityFlags & 0x02) // Matrix input
{
pkt << uint8(0);
pkt << uint8(0);
pkt << uint8(0);
pkt << uint8(0);
pkt << uint64(0);
}
if (securityFlags & 0x04) // Security token input
{
pkt << uint8(1);
}
我只是想弄清楚他们为什么使用pkt << uint32(0)
,因为它们对我来说似乎是完全多余的。而且他们也经常使用它,这更没有意义。
知道为什么他们的代码是这样写的吗?
运算符<<为 ByteBuffer 重载(这是一个 pkt 类型),它看起来如下:
https://github.com/mangostwo/server/blob/b8ce9508483375a36699c309bce36810c4548007/src/shared/ByteBuffer.h#L138
ByteBuffer& operator<<(uint8 value)
{
append<uint8>(value);
return *this;
}
所以它不是移位 0,而是附加值 0。
>pkt
很可能是一个重载operator<<
的类类型;如果不是这样,所有这些语句都不会产生任何影响,因为它的结果没有被使用。(pkt << 0
而不是pkt <<= 0
)。
相关文章:
- 使用boost::multiprecision cpp_int左移时出现超时错误
- 警告:左移计数 >= 在 C++ 中将字节流读取为双精度变量时的类型宽度
- 左移<<如何在不同的功能中给出不同的结果?
- 使用 AVX2 指令左移 128 位数字
- <<(按位左移)在 Swift 中的数组上做什么?
- 带有左移操作员C 的意外输出
- 编译器将输出的流运算符<<解释为用于按位左移的二进制运算符<<
- 左移负整数为零是否有意义?
- 在 VS 移位中,UINT64 上的左移(64 >移位> 32)位仅 32 位
- C++20 是否为"overflow"的有符号整数很好地定义了左移?
- C++左移操作
- 为什么编译器左移 0
- 如何在位明智的操作中选择正确的左移
- 为什么很多人使用左移<<而不是乘法
- 左移 0 有什么意义
- 虽然左移运算符 (<<) 在 std::cout 之前使用,但它有什么用?
- 左移<<有些意外的行为
- 使用左移运算符将 F 转换为 FF
- 按位左移一个带有 16 的无符号字符是什么意思
- c++中从整数到字符串的转换中,左移操作符是做什么的?