Table of Contents
The bitwise AND operator (&) performs a logical AND operation on each corresponding bit of two integral operands. A bit in the result is set to 1 only if the corresponding bits in both operands are 1; otherwise, the result bit is 0.
Bitwise operators are commonly used in systems programming, embedded programming, device drivers, and applications that manipulate individual bits.
Source Code #
#include <iostream>
int main()
{
int num1 = 12;
int num2 = 10;
int result = num1 & num2;
std::cout << "First Number : " << num1 << '\n';
std::cout << "Second Number: " << num2 << '\n';
std::cout << "Result : " << result << '\n';
return 0;
}
Output #
First Number : 12
Second Number: 10
Result : 8
Explanation #
- The bitwise AND operator (
&) compares the corresponding bits of two integral operands. - A result bit is set to
1only when both corresponding operand bits are1. - If either corresponding bit is
0, the resulting bit is0. - The operands
12and10have the following binary representations:12 = 1100â‚‚ 10 = 1010â‚‚ -------------- & = 1000â‚‚ - The binary result
1000â‚‚is equal to the decimal value8. - Bitwise AND operations are commonly used for bit masking and testing specific bits.
- The bitwise AND operator is applicable only to integral data types.
return 0;terminates the program successfully.