gpt4 book ai didi

c - 在 c 中的 uint64_t 中设置位

转载 作者:太空宇宙 更新时间:2023-11-04 06:11:39 26 4
gpt4 key购买 nike

您好,我想在某个位置(从左到右)设置一个位(值)。这是我的代码,它不适用于 uint64_t(这里它应该返回 0 而不是 1),但是当我更改值以使其与 uint8_t 一起工作(具有相同的逻辑)时,它可以工作。有任何想法吗?请。

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <inttypes.h>
int main()
{
uint64_t x = (uint64_t)1;
int pos = 63;
bool value = 0;
uint8_t z = x;
z = z >> (64-pos);
z = z << (64-pos);
uint64_t a = (uint64_t)1;
uint64_t d = (uint64_t)0;
uint64_t y;
uint64_t c = d;
uint64_t b = x;
b = b << (pos+1);
b = b >> (pos+1);
if (value == 1)
{
y = a;
y = y << (63-pos);
}
else
{
y=d;
}
c = c|z|y|b;
printf("%lld",c);
return c;
}

编辑:我认为有一个误解(我还不够清楚,我的错),实际上我有 x 是一个 uint64_t 并且我有一个 int pos 是 x 中一位的位置,我具有 bool 值(1 或 0),如果值为 1,则 x 中 pos 处的位必须变为/保持 1,如果值为 0,则 x 中 pos 处的位必须变为/保持为 0。

最佳答案

要在 value 中设置位 pos,您可以使用

value |= ((uint64_t)1) << pos;

您的其余代码应该做什么?

有关更新的问题,请参阅此 answer .

关于c - 在 c 中的 uint64_t 中设置位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54855093/

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