gpt4 book ai didi

c - 用 C 语言打印位集

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

所以,这是我的代码: http://pastebin.com/MejYmpvK

我正在尝试让输出显示此内容:

The BitSet is:
0000000001001010

Modified BitSet at index '14' = '1' is:
0010000001001010

The value at index '14' is:
1

Modified Bitset at index '14' = '0' is:
0000000001001010

The value at index '14' is:
0

但是,它给了我这个输出:

The BitSet is:
0000000001001010

Modified BitSet at index '14' = '1' is:
0010000001001010

The value at index '14' is:
0

Modified Bitset at index '14' = '0' is:
0000000001001010

The value at index '14' is:
0

除了显示方法之外,一切都工作正常。不确定这里发生了什么(整个代码位于上面的 Pastebin 链接中):

/* Returns the value of the bit at 'index' */
int bitValue(bitSet bs, int index){

/* Shifts right to the passed index value */
int value = ((bs & (int)pow(power, index)) >> index);

return value;
}

最佳答案

设置该位后,您的主要功能不会更新该值。试试这个;

int main () {
bitSet bits = makeBitSet();
int value = bitValue(bits, 14);

printf("The BitSet is: \n");
displayBitSet(bits);
printf("\n\nModified BitSet at index '14' = '1' is: \n");
setBit(&bits, 14);
displayBitSet(bits);
printf("\n\nThe value at index '14' is: \n");
value = bitValue(bits, 14);
printf("%d", value);
printf("\n\nModified BitSet at index '14' = '0' is: \n");
clearBit(&bits, 14);
displayBitSet(bits);
printf("\n\nThe value at index '14' is: \n");
value = bitValue(bits, 14);
printf("%d\n", value);

return (0);
}

关于c - 用 C 语言打印位集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35805435/

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