Storing multiple values via bitmask in c#

别等时光非礼了梦想. 提交于 2019-11-30 16:07:40

Without a more specific question, and in particular you showing the code you have so far and explaining what you're having trouble specifically in terms of getting it to work, it's hard to know exactly what the best answer would be.

That said, here are a couple of example methods that might get you pointed in the right direction:

// Stores the given value in storage at the given index
int Set(int storage, int value, int index)
{
    int shiftCount = index * 5,
        mask = 0x1f << shiftCount;

    return (storage & ~mask) | (value << shiftCount);
}

// Retrieves the value stored in storage at the given index
int Get(int storage, int index)
{
    int shiftCount = index * 5,
        mask = 0x1f << shiftCount;

    return (storage & mask) >> shiftCount;
}

The Set() method above takes the current value in storage, clears all of the bits in the range of bits where you want to store your five-bit value, and then uses the | operator to store that five-bit value, shifting the bits of that value to the right place first.

The Get() method performs the reverse operation. It masks off (clears) all of the bits not in the range of bits where the value was stored, and then shifting the stored bits down to the least-significant five bits of an int before returning that result.

Notes:

  • The above is specific to your stated problem. It could easily be generalized by encapsulating in a class where the bit count can be configured at initialization and the mask is generated based on that bit count rather than being hard-coded.
  • There is no error-checking in the above code. In a production-code version, it would be much better to verify that the value passed to the Set() method does in fact fit in five bits (i.e. is less than 0x20).

EDIT:

Here is a simple console program that demonstrates the use of the above, with your example data:

static void Main(string[] args)
{
    int[] array = { 31, 6, 23, 31 };
    int storage = 0;

    storage = ArrayToStorage(array, storage);

    Console.WriteLine(storage);
    LogArray(array);

    storage = Set(storage, 9, 1);
    storage = Set(storage, 17, 2);

    StorageToArray(array, storage);

    Console.WriteLine(storage);
    LogArray(array);
}

static int ArrayToStorage(int[] array, int storage)
{
    for (int i = 0; i < array.Length; i++)
    {
        storage = Set(storage, array[i], i);
    }

    return storage;
}

static void StorageToArray(int[] array, int storage)
{
    for (int i = 0; i < array.Length; i++)
    {
        array[i] = Get(storage, i);
    }
}

static void LogArray(int[] array)
{
    Console.WriteLine("[" + string.Join(", ", array) + "]");
}

// Stores the given value in storage at the given index
static int Set(int storage, int value, int index)
{
    int shiftCount = index * 5,
        mask = 0x1f << shiftCount;

    return (storage & ~mask) | (value << shiftCount);
}

// Retrieves the value stored in storage at the given index
static int Get(int storage, int index)
{
    int shiftCount = index * 5,
        mask = 0x1f << shiftCount;

    return (storage & mask) >> shiftCount;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!