![]() |
Logical And Bitwise AND
I was looking over the default code, and was confused on one part. Here is the code:
Code:
relay1_fwd = p1_sw_trig & rc_dig_in01; /* FWD only if switch1 is not closed. */ |
Re: Logical And Bitwise AND
Quote:
|
Re: Logical And Bitwise AND
Thanks. Wasn't sure how much Pic-C changed C.
|
Re: Logical And Bitwise AND
Bitwise AND can be used for the same purpose as logical AND. It looks like that is the case here.
|
Re: Logical And Bitwise AND
Quote:
The two are only interchangable here because both expressions of the operand are bits. If they weren't both bits you could get different results with the two operators. Bitwise AND looks at each bit of the two numbers and does a logical AND on the corresponding bits. Bits in the result are 1 if the corresponding bits in the two operands are both 1, else the bits are 0. Logical AND looks at the whole expression on each side. If both expressions are nonzero then the AND expression evaluates to TRUE (the value 1 in C). This snippet of code: Code:
int a = 0x03;Code:
1 2 |
Re: Logical And Bitwise AND
Quote:
Quote:
What he means is that a lot of time, either & bitwise or && logical can be used, because both operands are conditionals, thus they are both bits. |
Re: Logical And Bitwise AND
Even if he did, it was still ambiguous enough to require clarification.
|
Re: Logical And Bitwise AND
Sorry, didn't mean to cause confusion. All I was saying is that the two operands can be used to the same end. Obviously they are not interchangeable within the same statement and their technical function is very different.
|
Re: Logical And Bitwise AND
I always thought that & pointed to the address of a variable, example:
Code:
char *ptr;(Sorry if I'm wrong about this) |
Re: Logical And Bitwise AND
& has a number of functions, including those stated previously in this thread as well as what you just mentioned.
|
Re: Logical And Bitwise AND
what you're thinking of is & prefixed to something, which does give the address - & standing alone is the bitwise AND.
|
Re: Logical And Bitwise AND
Quote:
Quote:
What everyone is saying is analogous to saying that since 2*2 is equal to 4, and 2 + 2 is also equal to 4, the two operands can both be used to the same end. Just because an operation equates to the same number once does not mean they are the same. |
Re: Logical And Bitwise AND
Quote:
Your example is not quite what I had in mind, however, imagine it in base 2. |
Re: Logical And Bitwise AND
I think the matter has been clarified for even the casual reader now and all your purposes have been fullfilled.
The argument can and should probably die away now. The gist: C is a complex language and one line of code and operand can be used for many different things in many different circumstances. Know the differences between all of them, if you can express something in two different ways in C there's almost always a reason, know it. |
Re: Logical And Bitwise AND
And if I remember correctly, & can be used to make a reference to a variable or other thing, but doesn't require the pointer syntax. (NOTE: This is out of my C++ Book and I do not have any idea if it does go for C, it might)
Code:
int ival = 1024; |
Re: Logical And Bitwise AND
Quote:
|
Re: Logical And Bitwise AND
Quote:
Code:
int a = 5, b = 10; |
Re: Logical And Bitwise AND
Quote:
|
Re: Logical And Bitwise AND
Quote:
Things like this: Quote:
|
Re: Logical And Bitwise AND
Quote:
Unless someone selectively reads only certain parts of certain posts I don't think they will get the wrong idea. With that said, can we end this argument? |
Re: Logical And Bitwise AND
Quote:
The bottom line is, ALWAYS use && and || for logical comparison, and use & and | for bitwise operations. Don't deviate from this, and you shouldn't run into any problems. |
| All times are GMT -5. The time now is 19:52. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi