gpt4 book ai didi

bit-manipulation - 移位直到位串中的第一个 '1'

转载 作者:行者123 更新时间:2023-12-01 22:34:47 28 4
gpt4 key购买 nike

有没有办法移动我的位串,直到我将第一个 1 作为我的最低有效位?例如

0001000100 right shifts twice to 0000010001
0100001010 right shifts once to 0010000101
0010000000 right shifts seven times to 0000000001

我正在尝试通过按位运算来做到这一点。

最佳答案

如果 n 是某种无符号整数:

while ( (n & 0x1) == 0) n >>= 1;

关于bit-manipulation - 移位直到位串中的第一个 '1',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25757415/

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