Introduction
XOR helps flip specific bits in a number without changing others. It's like a switch that turns bits on or off.
variable = variable ^ mask;
int x = 0b00001111; x = x ^ 0b00000001; // toggles the last bit
int flags = 0b10101010; flags ^= 0b11110000; // toggles the top 4 bits
#include <stdio.h> int main() { unsigned char led_state = 0b00001111; // 4 LEDs on unsigned char toggle_mask = 0b00000101; // toggle LEDs 0 and 2 printf("Before toggle: 0x%02X\n", led_state); led_state = led_state ^ toggle_mask; printf("After toggle: 0x%02X\n", led_state); return 0; }
^) do when used to toggle bits in embedded C?flags using XOR in embedded C?unsigned char flags = 0b00001100;
flags = flags ^ 0b00000110;
printf("0x%02X", flags);status. What is the error?unsigned int status = 0x01; status = status & (1 << 1);
reg initially set to 0b10101010. You want to toggle bits 1, 3, and 5 simultaneously using XOR. Which mask should you use and what will be the final value of reg after toggling?Pass the test and this pattern moves unknown