The bitwise operators in C are & (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift) and >> (right shift). They work on the individual bits of integer operands, so you use them for flags, masks, and packing or unpacking values.
All six work on integer types only. You cannot apply them to float or double.
The six bitwise operators at a glance
| Operator | Name | Result bit is 1 when | Example (4-bit values) |
|---|---|---|---|
& | AND | both bits are 1 | 1100 & 1010 = 1000 |
| | OR | at least one bit is 1 | 1100 | 1010 = 1110 |
^ | XOR | the bits differ | 1100 ^ 1010 = 0110 |
~ | NOT | the bit was 0 | ~1100 = 0011 |
<< | left shift | bits move toward the high end | 0011 << 2 = 1100 |
>> | right shift | bits move toward the low end | 1100 >> 2 = 0011 |
The 4-bit examples are for readability. In real code the operands are at least int wide, so the leading bits are zeros (or ones, after ~).
AND, OR and XOR with a worked example
Take a = 12 (binary 1100) and b = 10 (binary 1010):
#include <stdio.h>
int main(void) {
unsigned a = 12; /* 1100 */
unsigned b = 10; /* 1010 */
printf("a & b = %u\n", a & b); /* 1000 = 8 */
printf("a | b = %u\n", a | b); /* 1110 = 14 */
printf("a ^ b = %u\n", a ^ b); /* 0110 = 6 */
return 0;
}
Example output, which you can reproduce by compiling the program:
a & b = 8
a | b = 14
a ^ b = 6
Per the C rules documented on cppreference, &, | and ^ first apply the usual arithmetic conversions to both operands and then apply the operation bit by bit. That means mixing a short and a long gives a long result.
What bitwise NOT (~) really returns
The tilde flips every bit, but the number of bits depends on the promoted type. Operands narrower than int are promoted to int first, so the result is wider than you might expect:
unsigned char small = 12;
printf("%d\n", ~small); /* -13: small is promoted to int, then flipped */
printf("%u\n", (unsigned char)~small); /* 243 */
For an unsigned type, ~x equals the maximum value of the type minus x. With a 32-bit unsigned int, ~0u is 4294967295 (all bits set). That is why ~0u is a common way to write "all ones" without hard-coding the width.
Left shift and right shift
A left shift by n multiplies by 2 to the power n, and a right shift divides by it, rounding toward zero for non-negative values:
unsigned x = 5;
printf("%u\n", x << 3); /* 40: 5 * 2^3 */
printf("%u\n", 40u >> 2); /* 10: 40 / 2^2 */
Unlike the logic operators, the shifts promote each operand separately, and the result takes the type of the promoted left operand. The right operand does not affect the result type.
Shifts have three rules that cause real bugs:
- Shift count out of range is undefined behavior. If the count is negative, or greater than or equal to the number of bits in the promoted left operand, the behavior is undefined. Shifting a 32-bit
unsignedby 32 is not a way to get zero. - Signed left shift that overflows is undefined behavior. For a non-negative signed value the result is the value times 2 to the power of the count only if that fits in the promoted type. Otherwise the behavior is undefined. Unsigned left shift is safe: bits shifted out are discarded, and the result is reduced modulo the maximum value plus 1.
- Right shift of a negative value is implementation-defined. Most implementations do an arithmetic shift, so
-1000 >> 1gives-500, but the language does not promise it.
The practical consequence: write 1u << 31, not 1 << 31. With a 32-bit int, the value 231 is not representable, so the signed form is undefined behavior while the unsigned form is well defined. Prefer unsigned types whenever you shift.
Setting, clearing, toggling and testing bits with masks
The most common use of the bitwise operators is a set of flags stored in one integer. Define each flag as a single bit with a left shift:
#define FLAG_READ (1u << 0) /* 001 */
#define FLAG_WRITE (1u << 1) /* 010 */
#define FLAG_EXEC (1u << 2) /* 100 */
unsigned perms = 0;
perms |= FLAG_READ | FLAG_WRITE; /* set: 011 = 3 */
perms &= ~FLAG_WRITE; /* clear: 001 = 1 */
perms ^= FLAG_EXEC; /* toggle: 101 = 5 */
if (perms & FLAG_READ) { /* test: non-zero means the bit is set */
puts("readable");
}
Each pattern has a reason:
- Set with
|=: OR with a 1 forces the bit on and leaves other bits alone. - Clear with
&= ~mask: the inverted mask has a 0 only in the target position, and AND with 0 forces the bit off. - Toggle with
^=: XOR with 1 flips a bit, and XOR with 0 keeps it. - Test with
&: the result is non-zero if any bit in the mask is set. To require that all bits of a multi-bit mask are set, compare with the mask:(perms & MASK) == MASK.
Extracting a byte from a larger value
Shift the wanted bits down to the bottom, then mask off everything above them:
uint32_t value = 0x12345678;
uint8_t second_byte = (value >> 8) & 0xFF; /* 0x56 */
Going the other way, shift each piece up and combine with OR: (a << 24) | (b << 16) | (c << 8) | d. Make sure the pieces are unsigned and wide enough, otherwise a << 24 on a plain uint8_t promotes to signed int and can overflow into the sign bit.
Small bit tricks worth knowing
- Odd or even:
n & 1is 1 for odd numbers and 0 for even ones. - Power of two: for an unsigned
n,n != 0 && (n & (n - 1)) == 0is true when exactly one bit is set. Forn = 8(1000),n - 1is0111, and the AND is 0. - Swap without a temporary:
a ^= b; b ^= a; a ^= b;works, but it breaks if both names refer to the same object (the value becomes 0), and a temporary variable is clearer.
Precedence pitfall: always parenthesize
In C, == binds tighter than &, which binds tighter than ^, then |, then && and ||. The shifts bind tighter than the comparison operators, and unary ~ binds tighter than everything binary. The classic bug:
unsigned x = 4;
if (x & 1 == 0) /* parsed as x & (1 == 0), i.e. x & 0: always false */
puts("even");
if ((x & 1) == 0) /* intended: true for 4 */
puts("even");
The safest habit is to put parentheses around every bitwise sub-expression that sits next to a comparison.
Bitwise versus logical operators
& and | look at bits, while && and || look at truthiness and stop evaluating early. 1 && 2 is 1, but 1 & 2 is 0, because 01 and 10 share no set bit. Using & where you meant && compiles without error and gives wrong answers for any value other than 0 and 1. The bitwise forms also always evaluate both operands, so p && p->ready is safe for a null pointer and p & p->ready is not. If you are working with addresses in the same code, see pointers in C explained with examples.
Compound assignment forms
Every binary bitwise operator has an assignment form: &=, |=, ^=, <<= and >>=. x |= y is shorthand for x = x | y, and it is the idiom used in the flag example above. There is no compound form for ~, since it takes only one operand.
Frequently asked questions
- What is the difference between & and && in C?
- The single & is bitwise AND: it compares two integers bit by bit and returns an integer. The double && is logical AND: it treats each operand as true or false and returns 1 or 0. For example, 1 && 2 is 1, but 1 & 2 is 0, because the two values share no set bits.
- What does ~ do in C?
- The tilde is bitwise NOT. It flips every bit of its operand after integer promotion. For an unsigned type, ~x equals the maximum value of that type minus x.
- Is right shift of a negative number safe in C?
- It is not undefined, but the result is implementation-defined. Most implementations perform an arithmetic shift, so the result stays negative, but portable code should not depend on that. Shift unsigned values when you want predictable behavior.
- Why does x & 1 == 0 give the wrong answer?
- The == operator has higher precedence than &, so the compiler reads it as x & (1 == 0), which is x & 0 and always 0. Write (x & 1) == 0 instead.