← Back to C Course | Chapter 3: Operators | Lesson 5 of 9

C Bitwise Operators

Bitwise operators work on the tiny 0s and 1s inside numbers, like flipping rows of light switches. They let you check or change individual switches.
Syntax
c
result = value1 & value2;  // AND
result = value1 | value2;  // OR
result = value1 ^ value2;  // XOR
result = ~value;           // NOT
result = value << n;       // shift left
result = value >> n;       // shift right

Bitwise AND (&)

& compares two numbers bit by bit and produces a 1 in each position where both operands have a 1, and a 0 everywhere else -- often used to check or isolate specific bits within a value, like reading configuration flags.

Example: Bitwise AND (&)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int a = 6, b = 3;
	// Print formatted text to the screen
	printf("%d", a & b);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Bitwise OR (|)

| compares two numbers bit by bit and produces a 1 in each position where at least one operand has a 1 -- commonly used to turn on specific bits without disturbing the others, such as setting a flag inside a bitmask.

Example: Bitwise OR (|)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int a = 6, b = 3;
	// Print formatted text to the screen
	printf("%d", a | b);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Bitwise XOR (^)

^ (XOR) produces a 1 in each bit position where the two operands differ, and a 0 where they match -- a classic use is toggling a bit on or off, or swapping two values without a temporary variable.

Example: Bitwise XOR (^)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int a = 6, b = 3;
	// Print formatted text to the screen
	printf("%d", a ^ b);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Bitwise NOT ()

~ is a unary operator that flips every bit of its single operand -- every 1 becomes 0 and every 0 becomes 1 -- which for a signed integer effectively produces its bitwise complement, not simply its negative.

Example: Bitwise NOT ()

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare a and set it to 5
	int a = 5;
	// Print formatted text to the screen
	printf("%d", ~a);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Shift Operators (<< and >>)

<< shifts all bits left by a given number of positions (equivalent to multiplying by a power of 2), while >> shifts them right (roughly equivalent to integer division by a power of 2) -- both are much faster than actual multiplication or division.

Example: Shift Operators (<< and >>)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare a and set it to 4
	int a = 4;
	// Print formatted text to the screen
	printf("%d %d", a << 1, a >> 1);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using & when you meant &&, which gives a bitwise result rather than a true/false test.
  2. Shifting by a negative amount or by more bits than the type has, which is undefined behavior.
  3. Using bitwise operators on signed negative numbers and being surprised by the result.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.