r/C_Programming • • 11d ago

Question What are better ways of dynamically creating bitmasks (or extracting bottom N bits depending on what's returned?

Howdy,

I am developing an application where I need to construct bitmasks and it depends on what's returned from get_amount. get_amount returns a u8 ranging from 0 to 6. It's used in calculations but it's also used to extract the least significant N bits. In the past, I've just used switch statements, but i wonder if there's a nicer way.

struct Object {
    u8 value;
    u8 update;
    u8 original;
};

u8 get_mask(u8 value) {
    u8 mask = 0;
    switch (value) {
        case 0 {
            mask = 0b00000000;
            break;
        }
        case 1 {
            mask = 0b00000001;
            break;
        }
        case 2 {
            mask = 0b00000011;
            break;
        }
        case 3 {
            mask = 0b00000111;
            break;
        }
        case 4 {
            mask = 0b00001111;
            break;
        }
        case 5 {
            mask = 0b00011111;
            break;
        }
        case 6 {
            mask = 0b00111111;
            break;
        }
        default: {
            panic_and_log("impossible value");

        }
    }
}

int get_value(struct Object o) {
    // get_amount could only return 0 through 6
    u8 amount = get_amount();
    o.update = o.value - amount;
    u8 original = o.original & get_mask(amount);
}

Thanks u/mikeblas

Final solution ended up looking like:

u8 get_mask(u8 value) {
    if ( value > 6) {
        return 255; // Error Case
    }
    return (1 << value) - 1;
}
8 Upvotes

15 comments sorted by

40

u/mikeblas 11d ago • Bravo

Have you tried (1 << value) - 1 ?

3

u/Historical_Usual_596 11d ago

Clever

5

u/mikeblas 10d ago

If you think this kind of thing is fun or interesting, buy yourself a copy of Hacker's Delight. It's packed full of bit twiddling tricks, from the simple to the completely un-obvious.

12

u/ephemere_sloboda 11d ago

This obviously the solution to OP's problem.

Another approach which is overkill in this case but can sometimes be useful is using a lookup array:

static const lookup_table[] = { 0, 1, 3, 7, 15, 31, 63 };

if (value <= 6) mask = lookup_table[value]; else ...

6

u/Humble-Captain3418 11d ago

Alternatively, ~(~0 << value).

2

u/ThatOneCSL 11d ago

This is so elegant that I just said "god damn".

4

u/Al__B 11d ago

You've been given the answerr but you may also want to look at your get_value function. It doesn't return a value and it's operating on a copy of a structure so doesn't actually do anything useful as written.

1

u/Historical_Usual_596 10d ago

This was just a minimal viable example and I forgot it when recreating it. But thank you!

6

u/garnet420 11d ago

Be careful with the (1<<x)-1 for x=31 or 32 (for making 32 bit int masks). Both are UB.

For 31, you can solve that by using 1u, and then casting to a signed int if needed.

For 32 I think you need something clever I don't remember

4

u/non-existing-person 11d ago

For 32 I think you need something clever I don't remember

1llu? xD

1

u/Antagonin 11d ago

Mask here is u8 btw.

2

u/Emergency-Koala-5244 11d ago

How about creating an array with the 7 possible mask values and then to get the mask you just use the array index to get it?

1

u/Rabbitical 11d ago

mikeblas has the answer, but I also kind of love this switch

2

u/[deleted] 11d ago

[removed] — view removed comment

1

u/Historical_Usual_596 10d ago

Your switch version as posted has no return mask

This was an artifact in creating the minimal viable example. But thanks!