gpt4 book ai didi

c - 重复移位

转载 作者:太空宇宙 更新时间:2023-11-04 05:53:21 24 4
gpt4 key购买 nike

我有一些代码可以从数字中提取位:

uint32_t foo = getValue(); /*this just returns a value for `foo`*/

while (foo){
foo <<= 2;
doSomethingWithFoo(foo);
}

我担心我在这里有未定义的行为,因为我最终会“过度移动”foo。我说得对吗?

最佳答案

您的代码是安全的。

那是因为几个类次的行为是明确定义的(只要每个类次的类次不太多)。本质上,您在每次迭代时都从一个全新的 foo 开始。

仅当您尝试一次性移动的位多于类型时才会出现问题。

关于c - 重复移位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33740769/

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