gpt4 book ai didi

c++ - 按位运算是否解析为常量值

转载 作者:行者123 更新时间:2023-11-30 01:16:08 25 4
gpt4 key购买 nike

我只是想知道是否有人知道按位 AND 和 OR 以及常量上的按位移位运算是否由编译器预先计算为常量值。

例如int Foo = Bar / ( 1 << 12 )

我这样做而不是 Bar >> 12 的原因是如果 Bar 是负值,它会下降到 -1 而不是零。

最佳答案

"I was just wondering if someone knew if bitwise ANDs and ORs aswell as bitwise shifts operating on constants gets pre-calculated by the compiler to constant values."

是的

( 1 << 12 )

部分将在编译时进行评估。

它实际上和写作一样

int Foo = Bar / 4096;

关于c++ - 按位运算是否解析为常量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27213053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com