The source code to demonstrate the bitwise AND (&) operator is given below. The given program is compiled and executed successfully.
# Ruby program to demonstrate the
# bitwise AND (&) operator
num1=5
num2=3
res =0
res = num1 & num2;
print num1," & ", num2," = ",res,"\n"
num1=7
num2=4
res = num1 & num2;
print num1," & ", num2," = ",res
Output:
5 & 3 = 1
7 & 4 = 4
Explanation:
In the above program, we created three integer variables num1, num2, res that are initialized with 5, 3, 0 respectively. Then we performed a bitwise "AND" operation using the "&" operator and printed the result using the print() function.
Evolution of expression:
res = 5 & 3
0101
0011
=====
0001
res = 1
res = 7 & 4
0111
0100
=====
0100
res = 4
Program/Source Code:
The source code to demonstrate the bitwise AND (&) operator is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we created three integer variables num1, num2, res that are initialized with 5, 3, 0 respectively. Then we performed a bitwise "AND" operation using the "&" operator and printed the result using the print() function.
Evolution of expression:
need an explanation for this answer? contact us directly to get an explanation for this answer