3

I am looking at the c code:

 if((VAR_ON&3) > 1)

I am not sure what kind of variable VAR_ON is, my guess is it is a pointer, but what does the &3 at the end do to it? I apologize if this is a duplicate question, I just could not find any question regarding the ampersand AFTER a variable.

TheBlindSpring
  • 631
  • 5
  • 22
  • I apologize, I was unaware there were referred to as bitwise operators and I did not know to look for that. Is there any other particular reason for the down votes? – TheBlindSpring Jun 23 '14 at 16:35
  • A better question would be: What does && mean in C++? – Jiminion Jun 23 '14 at 16:38
  • Not knowing this indicates that you're a rank amateur in C and in programming in general. As such you have to expect to take a few lumps when you ask a "dumb" question like that. (Hint: Find yourself a good C reference to look up this sort of thing.) – Hot Licks Jun 23 '14 at 16:39
  • @Jim That question would surely be duplicate, and that is not the question I am asking. I understand what && is, but that has a different meaning than &. – TheBlindSpring Jun 23 '14 at 16:39
  • If 3 was a variable, like int three = 3; then would if((VAR_ON &three) > 1) be ambiguous? – Jiminion Jun 23 '14 at 16:39
  • 3
    @HotLicks What makes this a "dumb" question? Isn't this website for learning? I understand the question has already been asked, I just could not find any of these questions. How else would I go about learning this if I did not know they were to referred to as bitwise operators? Even with a reference book, without knowing what is called, where do I look in the book. – TheBlindSpring Jun 23 '14 at 16:43
  • 2
    I put "dumb" in quotes for a reason -- the question *appears* "dumb" to those who have a modicum of C comprehension. But note that any halfway decent reference would have a table of operators where you could look up `&` to see what it means. Your failure to do such a simple reference does not make you look good. Learning to use references is a critical part of your education. – Hot Licks Jun 23 '14 at 17:19
  • I guess I did not think of the possability of the operators table. Thank you for direction in looking at reference books. – TheBlindSpring Jun 23 '14 at 17:29

3 Answers3

4

As used in the question code, the ampersand '&' is a bitwise 'and' operation.

Example (assuming that VAR_ON = '21'):

VAR_ON  21(Decimal)    00010101(Binary)
       & 3(Decimal)  & 00000011(Binary)
       ------------  ------------------
         1(Decimal)    00000001(Binary)

Hence if VAR_ON is '21', the expression (VAR_ON&3) will evaluate to '1'. The 'if' condition would be false:

if((VAR_ON&3) > 1)
Mahonri Moriancumer
  • 5,993
  • 2
  • 18
  • 28
1

& in that context will perform a bit wise AND operation.

So whatever VAR_ON is will be ANDed with 3 so that only the last 2 bits of the variable will be used in the comparison.

Tommy
  • 622
  • 5
  • 8
1

It's a bitwise AND. That's not an ampersand "after a variable". That's a bitwise binary operator working on two operands: VAR_ON and 3.