小编wie*_*iak的帖子

按位或的评估顺序

考虑以下代码:

#include <stdint.h>
#include <vector>
#include <iostream>
int main()
{
    typedef std::vector<uint8_t> rawbytes;
    rawbytes data({0xaa,0xbb,0xcc,0xdd});
    rawbytes::iterator data_it = data.begin();

    uint32_t as_int = (*data_it++ << 24 ) | ( *data_it++ << 16 ) | ( *data_it++ << 8 ) | *data_it++;     

    std::cout << std::hex << as_int << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

这段代码的输出是我所期望的:

aabbccdd
Run Code Online (Sandbox Code Playgroud)

实际上,我编写了类似的代码,但我意识到我不确定它是否可以正常工作。

我对此表示怀疑:

uint32_t as_int = (*data_it++ << 24 ) | ( *data_it++ << 16 ) | ( *data_it++ << 8 ) | *data_it++;
Run Code Online (Sandbox Code Playgroud)

如果可以保证从左到右执行评估,那么意味着按此顺序进行?:

1: (*data_it++ << 24 )
2: …
Run Code Online (Sandbox Code Playgroud)

c c++

2
推荐指数
1
解决办法
63
查看次数

标签 统计

c ×1

c++ ×1