Operator & , &&

hi i use this simple code but i don't undrstand why integer variable x=16 ?? can you explain me? thanks!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <conio.h>

using namespace std;


int main(void)
{
    int A=0, B=1,C=20,D=18,E=4;
    int F,x,y;

    F=A||B;
    x=C&D; // x =16 , why?
    y=C&&D;
    
    cout<<"A || B = "<<F<<"\n\n";
    cout<<"C & D = "<<x<<"\n\n";
    cout<<"C && D = "<<y<<"\n\n";
    
    cout<<"\n\nPress any key to continue...";
    getche();
    
    return 0;
}
Last edited on
The & operator is the bitwise AND operator.
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Bitwise_operators

Hope this helps.
the operators && and || are logical which means that they can return only 0 or 1. http://www.cplusplus.com/doc/tutorial/operators/


But when we use operator & or | , i can not understand how the result comes? help please ..
Last edited on
http://en.wikipedia.org/wiki/Bitwise_operation

|| and && are boolean, not logical. The term comes from boolean algebra, invented by George Boole.
Last edited on
&& is the logical AND and || is the logical OR , logical AND and logical OR are boolean -this is why they return 0 and 1.

But, i open this topic because my problem is that i don't understand how the result with operant & and | comes when we use integers?? how can i calculate the result ?

for example if we use

------example---------

C=20,D=18;
Y=C && D; // the result of Y = 16 , why? how can i calculate it ?

---------------------------------

if the question is stupid please tell me to know, thanks.
Last edited on
look here please .. http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/language/ref/logande.htm

at the end it says that " 1 & 4 evaluates to 0 " . but it doesn't explain how that comes. help me please..
Last edited on
Did you read the article Duoas and I posted?
thanks!
The bitwise & operator as already explained operates on bits.

so 1 & 4 is (assuming 32 bit integers):

00000000000000000000000000000001 //1
00000000000000000000000000000100 //4
----------------------------------------------------
00000000000000000000000000000000 //0 Result of binary &



remember that in binary logic:
1 AND 0 =0
1 AND 1 =1
i must thank everyone for the help and quick response. thank you very much.
Last edited on
Topic archived. No new replies allowed.