471,319 Members | 1,594 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

& 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??

Can anyone please explain.....
Jun 7 '07 #1
3 1253
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

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by jab3 | last post: by
21 posts views Thread by siliconwafer | last post: by
17 posts views Thread by Bruce One | last post: by
2 posts views Thread by Ian | last post: by
3 posts views Thread by Tony | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.