how operator( && or || works).....
int k=5|6|7; how it works ....
int k=4&5&6; how it work .....
Re: how operator( && or || works).....
it's bit-wise operation: OR | and the other AND &
Re: how operator( && or || works).....
Those are used to represent boolean operators. The && is used for AND statements and the || for OR statements.
The expression "A || B" will return true if either A or B where "A && B" will only return true if both A and B are true.
Code Java:
if(true || false)
//code will run
if(false || false)
//code will not run
if(true && false)
//code will not run
if(true && true)
//code will run
Re: how operator( && or || works).....
Quote:
Originally Posted by
Krumpir
Those are used to represent boolean operators. The && is used for AND statements and the || for OR statements.
The expression "A || B" will return true if either A or B where "A && B" will only return true if both A and B are true.
Code Java:
if(true || false)
//code will run
if(false || false)
//code will not run
if(true && false)
//code will not run
if(true && true)
//code will run
Sir, These Things i know .
But my question is different
e.g int k=5&4; give result k=4;
but i fail to decide the result of int k=4&2&6;
the same problem is with OR operator....
Re: how operator( && or || works).....
Basic OR logic:
x y x&y
--------------
1 0 | 0
1 1 | 1
0 1 | 0
0 0 | 0
In binary the numbers are represented as follow:
4: 100
2: 010
6: 110
The and operator will only return 1 when all 3 bits are 1.
4: 1 0 0
2: 0 1 0
6: 1 1 0
----------
0 0 0
Thus the value of 4&2&6 is zero.
Using the OR operator if only one bit needs to be 1 in order to return 1;
4: 1 0 0
2: 0 1 0
6: 1 1 0
----------
1 1 0
Thus the value of 4&2&6 is 6.
I hope you understand now, otherwise there is another explanation here: Bitwise operators in java
Re: how operator( && or || works).....
Quote:
Originally Posted by
Krumpir
Basic OR logic:
x y x&y
--------------
1 0 | 0
1 1 | 1
0 1 | 0
0 0 | 0
In binary the numbers are represented as follow:
4: 100
2: 010
6: 110
The and operator will only return 1 when all 3 bits are 1.
4: 1 0 0
2: 0 1 0
6: 1 1 0
----------
0 0 0
Thus the value of 4&2&6 is zero.
Using the OR operator if only one bit needs to be 1 in order to return 1;
4: 1 0 0
2: 0 1 0
6: 1 1 0
----------
1 1 0
Thus the value of 4&2&6 is 6.
I hope you understand now, otherwise there is another explanation here:
Bitwise operators in java
Thanks ......