1. Set or Clear a Specific Bit in a Register

Back To All Submissions
Previous Submission
Next Submission

Code

#include <stdio.h>

const unsigned char map[8] = {
    1, 2, 4, 8, 16, 32, 64, 128
};

unsigned char modifyBit(unsigned char reg, int pos, int mode) {
    if (pos > 7 || pos < 0) {
        return reg;
    }
    if (mode) {
        return reg | map[pos];
    }
    const unsigned char inv_map = ~(map[pos]);
    return inv_map & reg;
}

int main() {
    unsigned char reg;
    int pos, mode;
    scanf("%hhu %d %d", &reg, &pos, &mode);
    printf("%d", modifyBit(reg, pos, mode));
    return 0;
}

Solving Approach

We use a lookup table to find all of the bit positions for masking our reg. If we didn't have space for this, we could do 2^pos but here we optimize for time.

Once we isolate the bit, it's easy to set this bit as there is a 1 already in the position we need - so an or works great.

When we need to pull down the bit, we must make sure to preserve the rest of the data in the reg. We can invert the mask to get a 0 at the position we need. Then we and with the register ensuring that our bit position will be zero.

 

 

 

Was this helpful?
Upvote
Downvote