All submissions

Toggle the Bit in an 8-bit Register

Code

#include <stdio.h>
#include <stdint.h>

uint8_t toggle_bit(uint8_t reg, uint8_t pos) {
    // Your code here
    reg = reg^(1<<pos);
    return reg;
}

int main() {
    uint8_t reg, pos;
    scanf("%hhu %hhu", &reg, &pos);
    uint8_t result = toggle_bit(reg, pos);
    printf("%u", result);
    return 0;
}

Solving Approach

consider  example 2

Input: reg = 0000 0000, pos = 3 
Output: 0000 1000

 reg = reg^(1<<pos);

  1. (1<<pos) :- 1 is left shifted by specific position as given.

  2. reg^(1<<pos) :- here toggling with that specific position

 

   0000 0000       --> reg

^  0000 0100       --> 1<<pos

----------------------

   0000 0100       -> result

-----------------------

 

 

Loading...

Input

6 1

Expected Output

4