C Programming Language Tutorial
Variables and Data Types
Input/Output
Looping and Selection Structures
Array
Functions
Preprocessing Command
Pointer
Structure
File Operations
Important Knowledge
C Programming Language Bitwise Operations Tutorial
Bitwise operations are essential in low-level programming, as they allow you to manipulate individual bits in a number. This tutorial will guide you through the bitwise operations in the C programming language.
The bitwise AND operator compares each bit of the first operand with the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1; otherwise, the result bit is set to 0.
Syntax: result = a & b;
Example:
#include <stdio.h> int main() { int a = 12; // binary: 1100 int b = 25; // binary: 11001 int result; result = a & b; printf("a & b = %d\n", result); // Output: 8 (binary: 1000) return 0; }
The bitwise OR operator compares each bit of the first operand with the corresponding bit of the second operand. If either bit is 1, the corresponding result bit is set to 1; otherwise, the result bit is set to 0.
Syntax: result = a | b;
Example:
#include <stdio.h> int main() { int a = 12; // binary: 1100 int b = 25; // binary: 11001 int result; result = a | b; printf("a | b = %d\n", result); // Output: 29 (binary: 11101) return 0; }
The bitwise XOR operator compares each bit of the first operand with the corresponding bit of the second operand. If the bits are different, the corresponding result bit is set to 1; otherwise, the result bit is set to 0.
Syntax: result = a ^ b;
Example:
#include <stdio.h> int main() { int a = 12; // binary: 1100 int b = 25; // binary: 11001 int result; result = a ^ b; printf("a ^ b = %d\n", result); // Output: 21 (binary: 10101) return 0; }
The bitwise NOT operator inverts all the bits of the operand. If the bit is 1, it becomes 0, and if it's 0, it becomes 1.
Syntax: result = ~a;
Example:
#include <stdio.h> int main() { int a = 12; // binary: 1100 int result; result = ~a; printf("~a = %d\n", result); // Output: -13 (binary: 11111111111111111111111111110011) return 0; }
The bitwise left shift operator shifts the bits of the first operand to the left by the number of positions specified by the second operand. The vacant bit positions are filled with zeros.
Syntax: result = a << n;
Example:
#include <stdio.h> int main() { int a = 12; // binary: 1100 int n = 2; int result; result = a << n; printf("a << n = %d\n", result); // Output: 48 return 0; }
C program examples for bitwise AND operation:
#include <stdio.h> int main() { unsigned int a = 12; // Binary: 1100 unsigned int b = 25; // Binary: 11001 unsigned int result = a & b; // Bitwise AND printf("Result of bitwise AND: %u\n", result); return 0; }
Bitwise OR and XOR in C language:
#include <stdio.h> int main() { unsigned int a = 12; // Binary: 1100 unsigned int b = 25; // Binary: 11001 unsigned int resultOR = a | b; // Bitwise OR unsigned int resultXOR = a ^ b; // Bitwise XOR printf("Result of bitwise OR: %u\n", resultOR); printf("Result of bitwise XOR: %u\n", resultXOR); return 0; }
Using bitwise left shift in C:
#include <stdio.h> int main() { unsigned int num = 5; // Binary: 0101 unsigned int result = num << 2; // Bitwise left shift by 2 printf("Result of bitwise left shift: %u\n", result); return 0; }
Right shift operation in C programming:
#include <stdio.h> int main() { unsigned int num = 20; // Binary: 10100 unsigned int result = num >> 2; // Bitwise right shift by 2 printf("Result of bitwise right shift: %u\n", result); return 0; }