472,958 Members | 2,112 Online

# & and && operator confusion

100 100+
Hi,

I have a code...
Expand|Select|Wrap|Line Numbers
1.     int t=0x40000006,y=0x000000ff,i=0x40000000,a=0xf0000000;
2.     cout<<(t&y);
3.     cout<<"\n";
4.     if(m1=i&&(t&y))
5.         cout<<"Here"<<i&&(t&y);
6.     if(h=((t&a)==(m1=i&&(t&y))))
7.         cout<<"here"<<h<<endl<<m1;
8.     cin>>x;
9. }
10.

here cout<<"Here"<<i&&(t&y); gives 0x40000000 as output

My question is 0x40000000 && 0x00000006 gives 0x40000000...Why??

Jun 7 '07 #1
3 1444
DeMan
1,806 1GB
& is a bitwise operator
&& is a logical operator

For a logical operator there are only two values - true and false. Usually, c will treat 0 as false and ANYTHING else as true, so 0x40000000 && 0x00000006 will return true (since both are non-zero) (I don't think it's guaranteed to give 0x40000000, but it may do).

The bitwise operator doews a logical 'and' on EVERY bit thus
Expand|Select|Wrap|Line Numbers
1. 0x04 & 0x0c == 0x04
2.
Jun 7 '07 #2
rag84dec
100 100+
i am using visual studio 6.0 and my question is for && operator.
i&&(t&y) gives 0x40000000 y???
Jun 7 '07 #3
DeMan
1,806 1GB
because && return true or false - which can be implementation specific.

i&y makes 0x00000006 (as you expect)

0x40000000&& 0x00000006 (both are non zero) so the result is true (which is any non-zero value). In this particular case, the implementer has decided to return your first term
Jun 7 '07 #4