左移 0 有什么意义

What is the point of left shifting by 0?

本文关键字:什么 左移      更新时间:2023-10-16

我目前正在尝试为开源服务器实现谷歌身份验证器,他们有这么一小段代码

  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)。