Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Zero specific bit without bitmask?

I’d like to set a specific bit to 0 but without using a bitmask because I’m using enums and if the enum values change, enum fields get moved around then the bitmask would no longer be valid.

I can toggle a bit on, using a temporary variable then OR it with the already existing bitfield

#include <assert.h>

enum EState{
    k_EStateNone,// bit 1   - 1
    k_EStateFoo,//  bit 2   - 10
    k_EStateBar//   bit 3   - 100
};
uint64_t togglebiton(uint64_t state,EState ebit){
    uint64_t temp = 1<<ebit;
    state|=temp;
    return state;
}
int main(){
    uint64_t state = 0;
    state = togglebiton(state, k_EStateBar);
    state = togglebiton(state, k_EStateFoo);
    //foo bar = 110 == 6
    assert(state == 6);
    return 0;
}

But I’m unsure how to go about flipping the bit off, because I can’t AND the whole thing with 0..

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

To flip a bit off, first shift the bit to the desired location, then use a bitwise NOT to invert all bits so that all but the target bit is set, then do a bitwise AND.

uint64_t togglebitoff(uint64_t state,EState ebit){
    uint64_t temp = 1ULL<<ebit;
    state&=~temp;
    return state;
}

Also make sure that the constant you’re shifting is of the correct type so that you don’t shift into the sign bit of a signed type or more than the bit width of the type in question.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading