# how operator( && or || works).....

• July 19th, 2012, 01:54 AM
ashish12169
how operator( && or || works).....
int k=5|6|7; how it works ....
int k=4&5&6; how it work .....
• July 19th, 2012, 03:42 AM
Voodoo
Re: how operator( && or || works).....
it's bit-wise operation: OR | and the other AND &
• July 19th, 2012, 07:41 AM
Krumpir
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```
• July 19th, 2012, 08:20 AM
ashish12169
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....
• July 19th, 2012, 09:00 AM
Krumpir
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
• July 20th, 2012, 12:36 AM
ashish12169
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 ......