How to check if certain uint has 0x01000000 mask applied?

1

I do some reverse engineering. There is an uint value indicating slot number where something is stored. But there is different types of storages. When something stored in different storage the value has 0x01000000 mask applied. And when it stored in one more storage the mask is 0x02000000.

My function receive this value and I need to determine storage and slot. For example:

0x00000005 - is fifth slot in storage 0.

0x01000009 - is slot 9 in storage 1.

0x02000002 - is slot 2 in storage 2.

What is the best way to do so?

I currently do this:

uint test = 0x02000005;
bool bit_set_for_01 = (test & (1 << 24)) != 0;
bool bit_set_for_02 = (test & (1 << 25)) != 0;

But I feel this is not best thing to do.

c#
bit-fields
asked on Stack Overflow Aug 10, 2020 by Kosmo零

1 Answer

1

You could make it more readable and slightly more efficient by writing something like this:

public static (int slot, int storage) Storage(int flags)
{
    return ((flags >> 24) & 7, flags & 0xffff);
}

Then:

static void Main()
{
    var item = Storage(0x00000005);
    Console.WriteLine($"Storage: {item.storage}, slot: {item.slot}");

    // Or just for demo:
    Console.WriteLine(Storage(0x01000009));
    Console.WriteLine(Storage(0x02000002));
}

Note that I'm anding with 7 above, which gives you 3 bits for slot storage (0..7 slot numbers). I'm also anding with 0xffff which gives 16 bits for the slot.

You should adjust these values to reflect the number of bits you're using for the two numbers.

answered on Stack Overflow Aug 10, 2020 by Matthew Watson

User contributions licensed under CC BY-SA 3.0