Set or Clear a Specific Bit in a Register

Solving Approach

Setting a bit.

  1. Think of the number zero, all its bits are also zero right? 0 dec = 0b00000000 binary.
  2. Think of the number 1, only one of its bits is set,  1 dec = 0b00000001 binary.
  3. Think of the OR truth table, Now imagine A as the register and B as the number 1. This means that if we OR a register with the number 1, only the first bit will be modified.
  4. To move a bit we use the left shift operator. Summary: we can move the bit 1 using the left shift operator to the index we want to set. Then OR with this value to change this specific bit without affecting all the other bits.
  5. The solution becomes: |= (1<<position);

Clearing a bit.

  1. Think of the AND operator, if you AND a value with 1, the result will be 0 if the value was 0 or 1 if the value was 1.
  2. Now think of a value that has infinite number of 1's, in terms of the bits. Anything AND-ed with such a value will remain exactly the same. i.e all 0's will remain to be 0's and all 1's will remain to be 1's.
  3. Now we need to target a specific bit, so we left shift the number 1 = 0b00000001 to the bit we want to clear, lets say we want to clear bit 3, 1<<3 = 0b00000100, but wait... Our theory was to AND with 1's, So lets invert the value by using the bitwise not operator. ~. Therefore we need to invert the bits before AND-ing ~(1<<3).
  4. The solution becomes: &= ~(1<<position);

Code

#include <stdio.h>

unsigned char modifyBit(unsigned char reg, int pos, int mode) {
    // Write your code here
    if (mode){
        reg |= (1<<pos);
        return reg;
    }
    else{
        reg &= ~(1 << pos);
        return 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

Setting a bit.

  1. Think of the number zero, all its bits are also zero right? 0 dec = 0b00000000 binary.
  2. Think of the number 1, only one of its bits is set,  1 dec = 0b00000001 binary.
  3. Think of the OR truth table, Now imagine A as the register and B as the number 1. This means that if we OR a register with the number 1, only the first bit will be modified.
  4. To move a bit we use the left shift operator. Summary: we can move the bit 1 using the left shift operator to the index we want to set. Then OR with this value to change this specific bit without affecting all the other bits.
  5. The solution becomes: |= (1<<position);

Clearing a bit.

  1. Think of the AND operator, if you AND a value with 1, the result will be 0 if the value was 0 or 1 if the value was 1.
  2. Now think of a value that has infinite number of 1's, in terms of the bits. Anything AND-ed with such a value will remain exactly the same. i.e all 0's will remain to be 0's and all 1's will remain to be 1's.
  3. Now we need to target a specific bit, so we left shift the number 1 = 0b00000001 to the bit we want to clear, lets say we want to clear bit 3, 1<<3 = 0b00000100, but wait... Our theory was to AND with 1's, So lets invert the value by using the bitwise not operator. ~. Therefore we need to invert the bits before AND-ing ~(1<<3).
  4. The solution becomes: &= ~(1<<position);

 

Upvote
Downvote
Loading...

Input

10 3 1

Expected Output

10