Makahanap ng mga solusyon sa iyong mga problema gamit ang IDNStudy.com. Ang aming mga eksperto ay handang magbigay ng malalim na sagot at praktikal na solusyon sa lahat ng iyong mga tanong.

Let us assume that we have the following variables in our program:

int a = 2; int b = 5; int c = 3; int intResult = 0; boolean x = true; boolean y = false; boolean booleanResult = false;

What will be the value of intResult or booleanResult if we execute the following expressions?

intResult = a & b; ​


Sagot :

Answer: 0

int Result = a & b;

& means binary AND

Let as use this table:

1 & 1 = 1

0 & 0 = 0

0 & 1 = 0

1 & 0 = 0

Now convert a and b to binary.

2 & 5 = ?

0010 & 0101 = _ _ _ _

Solve:

0 & 0 = 0

0010 & 0101 = 0 _ _ _

0 & 1 = 0

0010 & 0101 = 0 0_ _

1 & 0= 0

0010 & 0101 = 0 0 0 _

0 & 1 =1

0010 & 0101 = 0 0 0 0

Now convert to decimal:

0 * 2^4 + 0 * 2^2 + 0 * 2^1 + 0 * 2^0 = 0

Answer:

int Result = a & b;

Result == 0