ELI5: Explain Like I'm 5

Bitwise operations in C

Bitwise operations in C refer to manipulating individual bits within binary numbers using logical operators.

Think of a binary number as a series of on and off switches. Each switch can have a value of 0 (off) or 1 (on).

In C, you can use bitwise operators to perform logical operations on these switches. The bitwise operators include AND (&), OR (|), XOR (^), NOT (~), left shift (<<), and right shift (>>).

AND (&) compares two values, and returns a value with a 1 in each position where both values have a 1. For example, if we perform the operation 0101 & 0011, we get 0001.

OR (|) compares two values, and returns a value with a 1 in each position where either value has a 1. For example, if we perform the operation 0101 | 0011, we get 0111.

XOR (^) compares two values, and returns a value with a 1 in each position where the two values are different. For example, if we perform the operation 0101 ^ 0011, we get 0110.

NOT (~) takes a single value and returns its complement, swapping 0s for 1s and 1s for 0s. For example, if we perform the operation ~0101, we get 1010.

Left shift (<<) shifts the bits of a value to the left by a specified number of places. For example, if we perform the operation 0101 << 2, we get 10100.

Right shift (>>) shifts the bits of a value to the right by a specified number of places. For example, if we perform the operation 0101 >> 2, we get 0001.

These operations are often used in low-level programming, such as in embedded systems and device driver programming. Bit masking and bit manipulation are also common use cases for bitwise operations.