C Bitwise Operators

 

C Bitwise Operators

Note: This is a more advanced topic in C. If you are new to programming, don't worry if it feels tricky at first - bitwise operators are mainly used in special cases like system programming, hardware control, or performance optimizations.

In C, bitwise operators let you work directly with the bits (the 1s and 0s) that make up numbers in binary form.

Every integer in a computer is stored in binary, which means it is represented using bits (binary digits) that are either 0 or 1. Bitwise operators allow you to compare, combine, shift, or flip these bits.

Note: Bitwise operations only work on integer types (such as intchar, or long).

List of Bitwise Operators

OperatorNameDescription
&ANDSets each bit to 1 if both bits are 1
|ORSets each bit to 1 if at least one of the bits is 1
^XORSets each bit to 1 if only one of the bits is 1
~NOTInverts all the bits
<<Left ShiftShifts bits to the left (multiplies by powers of 2)
>>Right ShiftShifts bits to the right (divides by powers of 2)

Binary Example

Let's start with two integers:

int a = 6;  // 0110 in binary
int b = 3;  // 0011 in binary

Here's how different bitwise operators work on these values:

OperationBinary ResultDecimal Result
a & b00102
a | b01117
a ^ b01015
~a...1001-7 (on most systems)
a << 1110012
a >> 100113

Decimal and Binary Values

Here is a small reference table showing how decimal numbers look in binary (16-bit format):

 0 = 0000000000000000
 1 = 0000000000000001
 2 = 0000000000000010
 3 = 0000000000000011
 4 = 0000000000000100
 5 = 0000000000000101
 6 = 0000000000000110
 7 = 0000000000000111
 8 = 0000000000001000
 9 = 0000000000001001
10 = 0000000000001010
11 = 0000000000001011
12 = 0000000000001100

Tip: Each step to the left doubles the value. For example, 0000000000000100 is 4 because the third bit from the right is set (22 = 4).


Bitwise AND (&)

The & operator compares each bit and returns 1 only if both bits are 1.

Example

int a = 6;   // 0110
int b = 3;   // 0011

int result = a & b;
printf("Result: %d\n", result); // 2 (0010)


Bitwise OR (|)

The | operator sets a bit to 1 if either bit is 1.

Example

int a = 6;   // 0110
int b = 3;   // 0011

int result = a | b;
printf("Result: %d\n", result); // 7 (0111)


Bitwise XOR (^)

The ^ operator returns 1 only when the bits are different.

Example

int a = 6;   // 0110
int b = 3;   // 0011

int result = a ^ b;
printf("Result: %d\n", result); // 5 (0101)


Bitwise NOT (~)

The ~ operator inverts each bit (0 becomes 1 and 1 becomes 0).

Example

int a = 5; // 00000101

int result = ~a;
printf("Result: %d\n", result); // -6 on most systems

Note: The result of ~ depends on how negative numbers are stored (usually two's complement). For example, 5 (00000101) becomes 11111010, which is interpreted as -6.


Left Shift (<<)

The << operator shifts bits to the left and fills in 0s on the right. This is the same as multiplying by powers of 2.

Example

int a = 3; // 00000011

int result = a << 2;
printf("Result: %d\n", result); // 12 (3 * 2^2)


Right Shift (>>)

The >> operator shifts bits to the right. This is the same as dividing by powers of 2. On signed integers, the sign bit may be preserved depending on the system.

Example

int a = 12; // 00001100

int result = a >> 2;
printf("Result: %d\n", result); // 3 (12 / 2^2)


Real-Life Example: Flags and Permissions

Bitwise operators are often used to store multiple options in a single integer, using flags.

Example

#define READ  1  // 0001
#define WRITE 2  // 0010
#define EXEC  4  // 0100

int permissions = READ | WRITE;  // user can read and write

if (permissions & READ) {
  printf("Read allowed\n");
}
if (permissions & WRITE) {
  printf("Write allowed\n");
}
if (permissions & EXEC) {
  printf("Execute allowed\n");
}

In this example, the user has both READ and WRITE permissions, but not EXEC.


Summary

  • Bitwise operators work on individual bits of integers
  • &: AND - both bits must be 1
  • |: OR - either bit can be 1
  • ^: XOR - only one bit is 1
  • ~: NOT - flips all bits
  • <<: Left shift - multiplies by powers of 2
  • >>: Right shift - divides by powers of 2

Tip: Bitwise operations are useful for optimization, low-level hardware access, flags, and masks.


0 Comments