Bitwise Operators in C
Bitwise operators allow you to perform operations on individual bits of integers. These operators are useful for low-level operations, such as manipulating hardware registers or implementing data structures.
#include <stdio.h>
int main() {
unsigned int num1 = 10; // 0000 1010
unsigned int num2 = 5; // 0000 0101
unsigned int result1 = num1 & num2; // Bitwise AND
unsigned int result2 = num1 | num2; // Bitwise OR
unsigned int result3 = num1 ^ num2; // Bitwise XOR
unsigned int result4 = ~num1; // Bitwise NOT
unsigned int result5 = num1 << 2; // Left shift
unsigned int result6 = num1 >> 1; // Right shift
printf("Result 1: %u\n", result1);
printf("Result 2: %u\n", result2);
printf("Result 3: %u\n", result3);
printf("Result 4: %u\n", result4);
printf("Result 5: %u\n", result5);
printf("Result 6: %u\n", result6);
return 0;
}
In this example, bitwise operators are used to perform various bitwise operations on two unsigned integers. The results are displayed using printf.
No comments:
Post a Comment