# Thread: The difference between & and && or | and ||

1. ## The difference between & and && or | and ||

Please can anybody tell me the difference between & and && , | and || with example .

thank you.

2. && is logical and.
true && true is true and everything else is false.
& is bitwise and.
(10 & 8) = 8
It can be explained as binary:
Code:
``` 00000110
&00000100
=00000100```
and
(83 & 15) = 3
Code:
``` 00101011
&00000111
=00000011```
And the same thing with bitwise or (| operator):
(83 | 15) = 95
Code:
``` 00101011
|00000111
=00101111```
That means every bit is compared.

http://www.cprogramming.com/tutorial...operators.html

3. Logical operators work with two values - true (1) and false (0), and returns a boolen value.
Bitwise operations sort of perform the logical operation for each pair of bits in a char or int for example.

The results needn't be "similar" at all. See this:
Code:
```#include <iostream>

int main()
{
unsigned int a = 16, b = 15;
//display bool values as true/false
std::cout << std::boolalpha;
std::cout << "Logical " << a << "&&" << b <<
": " << (a && b) << std::endl <<
"Bitwise " << a << "&" << b <<
" : " << (a & b) << std::endl;
std::cin.get();
}```

4. thank you all , its now clear to me

but in a condition IF , we should use && and || so as to result a logical value , true ?

5. For most common tasks, yes.

Unless you specifically want to test if a bitwise operation returns 0 or non-zero.
For example you can take advantage of the fact that n&(n-1) equals 0 only if n is a power of 2 to do something like that:
Code:
```#include <iostream>

int main()
{
for (unsigned int i = 1; i <= 64; i++) {
if (i&(i-1)) {
std::cout << i << ": not power of 2" << std::endl;
}
else {
std::cout << i << ": power of 2" << std::endl;
}
}
std::cin.get();
}```

6. yes , its now completely clear to me . thank you anon

7. With Bitwise operation Carry bit generated but it don't add up with next bit, for ex: 0x0010 | 0x1010 = 0x1010
here 2nd bit addition 1+1 = 1 and carry =1, which doesn't get added with 3rd bit.

So its operation fairly with individual bit.

8. Acquaint yourself with the rules before digging up threads many years old.