AND, OR, XOR, and NOT Operators
Bitwise operators are essential tools in C for directly manipulating the individual bits of integer values. The four fundamental bitwise operators are AND (&), OR (|), XOR (^), and NOT (~). Each operator works at the bit level, applying its logic to corresponding bits in the operands. Understanding these operators begins with their truth tables, which describe how each operator combines or transforms bits:
Produces a 1 only if both bits are 1; otherwise, the result is 0.
Produces a 1 if either bit is 1; the result is 0 only if both bits are 0.
Produces a 1 if the bits are different; if they are the same, the result is 0.
Flips every bit: 1 becomes 0, and 0 becomes 1.
Here are the truth tables for the three binary operators:
For the unary NOT operator:
These operators allow you to set, clear, toggle, and test individual bits within integer values, which is a powerful technique for low-level programming and hardware control.
main.c
123456789101112131415161718192021222324252627282930313233343536373839#include <stdio.h> // Helper function to print an integer in binary (8 bits) void print_binary(unsigned int n) { for (int i = 7; i >= 0; i--) { printf("%u", (n >> i) & 1); } } int main() { unsigned int a = 42; // 00101010 unsigned int b = 15; // 00001111 printf("a = %3u (", a); print_binary(a); printf(")\n"); printf("b = %3u (", b); print_binary(b); printf(")\n"); printf("a & b = %3u (", a & b); print_binary(a & b); printf(") // AND\n"); printf("a | b = %3u (", a | b); print_binary(a | b); printf(") // OR\n"); printf("a ^ b = %3u (", a ^ b); print_binary(a ^ b); printf(") // XOR\n"); printf("~a = %3u (", (unsigned int)(~a & 0xFF)); print_binary(~a & 0xFF); printf(") // NOT (showing only 8 bits)\n"); return 0; }
Bitwise operators are used when you need precise control over binary data. By observing the program output, you can see how each operator changes the bits of integers a and b. The AND operator (a & b) is commonly used to mask bits and check specific flags, while the OR operator (a | b) is used to set bits and enable features.
The XOR operator (a ^ b) helps toggle bits or detect differences between values, and the NOT operator (~a) inverts all bits to create masks or perform bitwise negation. Together, these operators are the basis for techniques like flag management, bit masking, and efficient data representation.
Danke für Ihr Feedback!
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
Can you give examples of how to use each bitwise operator in C?
What are some common real-world applications of bitwise operators?
How does two's complement affect the result of the NOT operator?
Großartig!
Completion Rate verbessert auf 6.25
AND, OR, XOR, and NOT Operators
Swipe um das Menü anzuzeigen
Bitwise operators are essential tools in C for directly manipulating the individual bits of integer values. The four fundamental bitwise operators are AND (&), OR (|), XOR (^), and NOT (~). Each operator works at the bit level, applying its logic to corresponding bits in the operands. Understanding these operators begins with their truth tables, which describe how each operator combines or transforms bits:
Produces a 1 only if both bits are 1; otherwise, the result is 0.
Produces a 1 if either bit is 1; the result is 0 only if both bits are 0.
Produces a 1 if the bits are different; if they are the same, the result is 0.
Flips every bit: 1 becomes 0, and 0 becomes 1.
Here are the truth tables for the three binary operators:
For the unary NOT operator:
These operators allow you to set, clear, toggle, and test individual bits within integer values, which is a powerful technique for low-level programming and hardware control.
main.c
123456789101112131415161718192021222324252627282930313233343536373839#include <stdio.h> // Helper function to print an integer in binary (8 bits) void print_binary(unsigned int n) { for (int i = 7; i >= 0; i--) { printf("%u", (n >> i) & 1); } } int main() { unsigned int a = 42; // 00101010 unsigned int b = 15; // 00001111 printf("a = %3u (", a); print_binary(a); printf(")\n"); printf("b = %3u (", b); print_binary(b); printf(")\n"); printf("a & b = %3u (", a & b); print_binary(a & b); printf(") // AND\n"); printf("a | b = %3u (", a | b); print_binary(a | b); printf(") // OR\n"); printf("a ^ b = %3u (", a ^ b); print_binary(a ^ b); printf(") // XOR\n"); printf("~a = %3u (", (unsigned int)(~a & 0xFF)); print_binary(~a & 0xFF); printf(") // NOT (showing only 8 bits)\n"); return 0; }
Bitwise operators are used when you need precise control over binary data. By observing the program output, you can see how each operator changes the bits of integers a and b. The AND operator (a & b) is commonly used to mask bits and check specific flags, while the OR operator (a | b) is used to set bits and enable features.
The XOR operator (a ^ b) helps toggle bits or detect differences between values, and the NOT operator (~a) inverts all bits to create masks or perform bitwise negation. Together, these operators are the basis for techniques like flag management, bit masking, and efficient data representation.
Danke für Ihr Feedback!