View Full Version : Logical And Bitwise AND
SeanCassidy
12-01-2004, 19:42
I was looking over the default code, and was confused on one part. Here is the code:
relay1_fwd = p1_sw_trig & rc_dig_in01; /* FWD only if switch1 is not closed. */
Now knowing regular C, I would say & is bitwise AND. But then I saw the comment above it, "The & used below is the C symbol for AND." And I wasn't sure anymore if it was logical or bitwise AND. Could someone clarify for me?
jeremy562
12-01-2004, 19:47
I was looking over the default code, and was confused on one part. Here is the code:
relay1_fwd = p1_sw_trig & rc_dig_in01; /* FWD only if switch1 is not closed. */
Now knowing regular C, I would say & is bitwise AND. But then I saw the comment above it, "The & used below is the C symbol for AND." And I wasn't sure anymore if it was logical or bitwise AND. Could someone clarify for me?
It's the symbol for bitwise AND. Logical AND in C is always &&.
SeanCassidy
12-01-2004, 19:48
Thanks. Wasn't sure how much Pic-C changed C.
Bitwise AND can be used for the same purpose as logical AND. It looks like that is the case here.
Mike Soukup
16-01-2004, 17:38
Bitwise AND can be used for the same purpose as logical AND. It looks like that is the case here.
Absolutely NOT TRUE. Again as I've said in previous posts, if you do not know what you're talking about, do not pretend that you do and make untrue assertions. It's best to keep quiet and not confuse people who may believe what you post.
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:
int a = 0x03;
int b = 0x06;
printf("%d %d\n", (a && b), (a & b));
prints:
1 2
Why? (a && b) evaluates to TRUE because both numbers are nonzero, so we see a 1. (a & b) in binary is (0011 & 1010) and it evaluates to 0010, or 2.
Skabana159
16-01-2004, 18:06
Bitwise AND can be used for the same purpose as logical AND. It looks like that is the case here.
Absolutely NOT TRUE.
Easy now! He said that it CAN BE USED for the same purpose, not that they are the same thing.
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.
Dave Scheck
16-01-2004, 18:18
Even if he did, it was still ambiguous enough to require clarification.
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.
Raven_Writer
16-01-2004, 19:15
I always thought that & pointed to the address of a variable, example:
char *ptr;
printf("%s", &ptr);
I've never used pointers much/at all before, but I this is what I remember reading.
(Sorry if I'm wrong about this)
& has a number of functions, including those stated previously in this thread as well as what you just mentioned.
deltacoder1020
17-01-2004, 01:03
what you're thinking of is & prefixed to something, which does give the address - & standing alone is the bitwise AND.
Mike Soukup
17-01-2004, 15:58
Easy now! He said that it CAN BE USED for the same purpose, not that they are the same thing.
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.
Not true. In almost every instance & and && can not be used for the same purpose. The only time they are guaranteed to return the same result is if the two operands are bits.
All I was saying is that the two operands can be used to the same end.
Absolutely not true again. They have different meanings. They translate into different machine instructions. They give the same result only for very limited scope.
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.
Not true. In almost every instance & and && can not be used for the same purpose. The only time they are guaranteed to return the same result is if the two operands are bits.
Absolutely not true again. They have different meanings. They translate into different machine instructions. They give the same result only for very limited scope.
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.
And what you are saying is that what we are saying is that they are the same. This is absolutely not true. No one in this thread stated that they are the same.
Your example is not quite what I had in mind, however, imagine it in base 2.
djcapelis
17-01-2004, 16:32
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.
SeanCassidy
18-01-2004, 00:22
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)
int ival = 1024;
int &refVal = ival; // ok: refVal is a reference to ival
int &refVal2; // no: refVal2 needs to be initalized.
Not wanting to bring up the flame war on this "heated" discussion (hehe), but just so you know ampersand (&) is the address-of operator, bitwise AND, logical AND (in pairs, &&), and reference operator. So watch those &'s! ;)
Dave Flowerday
18-01-2004, 01:51
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)
There is no concept of references in C - it's an enhancement added by C++.
All I was saying is that the two operands can be used to the same end.
Look at this:
int a = 5, b = 10;
if (a && b) { /* execute this block of code */ }
if (a & b) { /* now execute this block of code */ }
You'll note that (a&&b) evaluates to true, because both a and b are non-zero. However, and this is fairly important, (a&b) evaluates to false, because (a&b) is (0101b&1010b) is 0. (And as far as address-of operator, remember that in this context & AND && are binary operators.)
Look at this:
int a = 5, b = 10;
if (a && b) { /* execute this block of code */ }
if (a & b) { /* now execute this block of code */ }
You'll note that (a&&b) evaluates to true, because both a and b are non-zero. However, and this is fairly important, (a&b) evaluates to false, because (a&b) is (0101b&1010b) is 0. (And as far as address-of operator, remember that in this context & AND && are binary operators.)
I'm not saying that they are interchangeable, because obviously they are not! However, just as war and diplomacy can be used to the same end, && and & can also.
Dave Flowerday
18-01-2004, 15:25
However, just as war and diplomacy can be used to the same end, && and & can also.
We all understand what you're trying to say. What you're saying is that in one particular case, they happen to give the same answer. However, the manner in which you keep saying it is very misleading and implies that they can be used interchangeably. Please keep in mind that many inexperienced people come here looking for advice. The way you've worded several posts in this thread implies that & and && can be used interchangeably, which isn't true.
Things like this:
Bitwise AND can be used for the same purpose as logical AND.
Most people would read this to mean that & and && can be used "for the same purpose" which is not true, at least not in the generic case. On a job, if you use the wrong AND (even if it gives the same result!) you will be told to fix it.
We all understand what you're trying to say. What you're saying is that in one particular case, they happen to give the same answer. However, the manner in which you keep saying it is very misleading and implies that they can be used interchangeably. Please keep in mind that many inexperienced people come here looking for advice. The way you've worded several posts in this thread implies that & and && can be used interchangeably, which isn't true.
Things like this:
Most people would read this to mean that & and && can be used "for the same purpose" which is not true, at least not in the generic case. On a job, if you use the wrong AND (even if it gives the same result!) you will be told to fix it.
And since then I have said that they are not interchangeable about 3 or 4 times.
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?
jeremy562
22-01-2004, 16:35
And since then I have said that they are not interchangeable about 3 or 4 times.
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?
The specific phrase that is misleading is that they can both be used "to the same end". To me, this phrase implies that they do the same thing, which you agree is not the case. Obviously, it's not worth arguing over because (a) no good can possibly come from it, and (b) everyone is in agreement ANYWAY.
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.
vBulletin® v3.6.4, Copyright ©2000-2017, Jelsoft Enterprises Ltd.