Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer AND, OR, XOR, and NOT Operators | Bitwise Operators and Shifts
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Bitwise Operations and Binary Logic in C

bookAND, 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:

Bitwise AND (`&`)
expand arrow

Produces a 1 only if both bits are 1; otherwise, the result is 0.

Bitwise OR (`|`)
expand arrow

Produces a 1 if either bit is 1; the result is 0 only if both bits are 0.

Bitwise XOR (`^`)
expand arrow

Produces a 1 if the bits are different; if they are the same, the result is 0.

Bitwise NOT (`~`)
expand arrow

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

main.c

copy
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.

question mark

Which statements accurately describe the behavior of the bitwise AND (&), OR (|), XOR (^), and NOT (~) operators in C

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 1

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

bookAND, OR, XOR, and NOT Operators

Veeg om het menu te tonen

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:

Bitwise AND (`&`)
expand arrow

Produces a 1 only if both bits are 1; otherwise, the result is 0.

Bitwise OR (`|`)
expand arrow

Produces a 1 if either bit is 1; the result is 0 only if both bits are 0.

Bitwise XOR (`^`)
expand arrow

Produces a 1 if the bits are different; if they are the same, the result is 0.

Bitwise NOT (`~`)
expand arrow

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

main.c

copy
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.

question mark

Which statements accurately describe the behavior of the bitwise AND (&), OR (|), XOR (^), and NOT (~) operators in C

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 1
some-alt