By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

& and && operator confusion

P: 100

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. }

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
Share this Question
Share on Google+
3 Replies

P: 1,806
& 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
Jun 7 '07 #2

P: 100
i am using visual studio 6.0 and my question is for && operator.
i&&(t&y) gives 0x40000000 y???
Jun 7 '07 #3

P: 1,806
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.