简体   繁体   English

在 128 位整数数组中发出清除位

[英]Issue clearing bits in 128 bit integer array

My goal with this program is that I want to be able to set 1000 bits to 0 or 1 separately, for this I'm using an array of 128 bit integers.我对这个程序的目标是我希望能够将 1000 位分别设置为 0 或 1,为此我使用了一个 128 位整数数组。

Now the issue is when I simply clear_bit(3), bit 35 is also being cleared (and vice versa).现在的问题是当我只是 clear_bit(3) 时,第 35 位也被清除(反之亦然)。 3 & 35 are always cleared together, same for 4 & 36, 5 & 37 etc. So there's clearly a pattern. 3 和 35 总是一起清零,4 和 36、5 和 37 等也一样。所以显然有一个模式。 I simply want 3 to be cleared while the rest stays 1.我只想清除 3,而其余的保持 1。

Any ideas why this is happening?任何想法为什么会发生这种情况? Thanks!谢谢!

#include <stdio.h>

__uint128_t array [(1000/128) + 1];

// Set bit to 0.
void clear_bit(int k)                
{
    array[k/128] &= ~(1 << (k%128));
}

// Find the value of bit.
int test_bit(int k)
{
    return((array[k/128] & (1 << (k%128) )) != 0);     
}

// Set bit to 1.
void set_bit(int k)
{
    array[k/128] |= 1 << (k%128);  // Set the bit at the k-th position in A[i]
}

int main (void)
{
    // Set all bits to 1.
    for(int i = 0; i < 40; i++) {
        set_bit(i);
    }

    // I want to clear bit 3, but it also clears 35 for an unknown reason.
    clear_bit(3);

    for(int i = 0; i < 40; i++) {
        printf("%d is now:%d\n", i, test_bit(i));
    }

    return (0);
}

结果

OP's code is not using 128-bit math with 1 << (k%128) . OP 的代码没有使用1 << (k%128) 128 位数学。 @user2357112 @用户2357112

Insure the integer math is done with at least 128-bit math.确保整数数学至少使用 128 位数学完成。

array[k/128] &= ~(1 << (k%128));
array[k/128] &= ~((__uint128_t)1 << (k%128));

Alternative, use unsigned in a portable fashion should unsigned have a bit width of be 16,32 64, 36, etc. No need to rely on __uint128_t .或者,以可移植方式使用unsigned应该unsigned的位宽为 16,32 __uint128_t等。无需依赖__uint128_t

#include <limits.h>
#define UNS_WIDTH (sizeof(unsigned)*CHAR_BIT)

unsigned array[(1000 + UNS_WIDTH - 1)/UNS_WIDTH];

void clear_bit(int k) {
  array[k/UNS_WIDTH] &= ~(1u << (k%UNS_WIDTH));
}

Best to insure the 1 is unsigned with 1u .最好确保11u unsigned

I would suggest using the native size of platform registers.我建议使用平台寄存器的本机大小。 just compare the generated code (in this example 64 bit platform) - https://godbolt.org/g/Y316vU只需比较生成的代码(在此示例中为 64 位平台)- https://godbolt.org/g/Y316vU

__uint128_t array [(1000/128) + 1];
uint64_t array1[(1000/64) + 1];

void assignBit64(unsigned bit, unsigned value)
{
    array1[bit >> 6] &= ~((uint64_t)1 << (bit & 63));
    array1[bit >> 6] |= ((uint64_t)!!value) << (bit & 63);
}

void assignBit128(unsigned bit, unsigned value)
{
    array[bit >> 7] &= ~((__uint128_t)1 << (bit & 127));
    array[bit >> 7] |= ((__uint128_t)(!!value)) << (bit & 127);
}


void resetBit64(unsigned bit)
{
    array1[bit >> 6] &= ~((uint64_t)1 << (bit & 63));    
}

void resetBit128(unsigned bit)
{
    array[bit >> 7] &= ~((__uint128_t)1 << (bit & 127));
}

void setBit64(unsigned bit)
{
    array1[bit >> 6] |= ((uint64_t)1 << (bit & 63));    
}

void setBit128(unsigned bit)
{
    array[bit >> 7] |= ((__uint128_t)1 << (bit & 127));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM