gpt4 book ai didi

c++ - 在 C++ 中制作特定位 "0"

转载 作者:可可西里 更新时间:2023-11-01 15:05:29 25 4
gpt4 key购买 nike

<分区>

我是编程新手。最近我遇到了一个问题,我必须将数字的特定位设为 0。

例如:

我有一个数字p

p      = 73
binary = 1001001

现在我想把第 4 位设为 0,即 1000001(2) = 65(10)

我是通过以下方式做到的:

int p = 73;
int pos = 1<<3; // 4th bit
int max_bit = (1<<31) - 1; // making all bit to 1
int mask = pos ^ max_bit; // making 4th bit to 0 except others
p = p & mask; // changing 4th bit of p to 0
cout<<p<<endl;

有更好的方法吗?

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