#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", ®, &pos); uint8_t result = toggle_bit(reg, pos); printf("%u", result); return 0; }
consider example 2
Input: reg = 0000 0000, pos = 3 Output: 0000 1000
reg = reg^(1<<pos);
(1<<pos) :- 1 is left shifted by specific position as given.
(1<<pos)
reg^(1<<pos) :- here toggling with that specific position
reg^(1<<pos)
0000 0000 --> reg
^ 0000 0100 --> 1<<pos
1<<pos
----------------------
0000 0100 -> result
-----------------------
Test Cases
Test Results
Input
6 1
Expected Output
4