Please can anybody tell me the difference between & and && , | and || with example .
thank you.
Please can anybody tell me the difference between & and && , | and || with example .
thank you.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
&& is logical and.
true && true is true and everything else is false.
& is bitwise and.
(10 & 8) = 8
It can be explained as binary:
andCode:00000110 &00000100 =00000100
(83 & 15) = 3
And the same thing with bitwise or (| operator):Code:00101011 &00000111 =00000011
(83 | 15) = 95
That means every bit is compared.Code:00101011 |00000111 =00101111
http://www.cprogramming.com/tutorial...operators.html
Read about other bitwise operators.
Last edited by maxorator; 11-08-2006 at 03:18 PM.
"The Internet treats censorship as damage and routes around it." - John Gilmore
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(); }
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 ?
Last edited by GSalah; 11-09-2006 at 04:19 AM.
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(); }
yes , its now completely clear to me . thank you anon
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.
Acquaint yourself with the rules before digging up threads many years old.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.