Dive into low-level data manipulation with bitwise AND, OR, XOR, NOT, left shift, and right shift.
Bitwise operators are powerful tools in C that allow you to perform operations on the individual bits of integer data. Unlike logical operators that work on boolean true/false values, bitwise operators manipulate the binary representation of numbers. These are critical in systems programming, embedded systems, and performance-critical applications. The primary bitwise operators are: Bitwise AND (`&`) compares each bit of two numbers and sets the corresponding bit in the result to 1 only if both bits are 1. Bitwise OR (`|`) compares each bit and sets the result bit to 1 if at least one of the bits is 1. Bitwise XOR (`^`) (exclusive OR) sets the result bit to 1 only if the two bits are different. Bitwise NOT (`~`) is a unary operator that inverts all the bits of a number (0s become 1s and 1s become 0s). The Shift operators move the bits of a number to the left or right. Left Shift (`<<`) shifts the bits to the left by a specified number of positions, filling the empty positions on the right with zeros. This is equivalent to multiplying the number by 2 for each position shifted. Right Shift (`>>`) shifts the bits to the right. For unsigned numbers, the empty positions on the left are filled with zeros. For signed numbers, the behavior can be implementation-defined (either sign-extended or zero-filled). Right shifting is equivalent to dividing the number by 2 for each position shifted. These operators are used for tasks like setting or clearing specific bits (bit masking), checking the status of a bit, or performing fast multiplication/division by powers of 2.