View Full Version : joystick and extra buttons
TEAM1949
18-02-2008, 08:58
I am trying to add some buttons. I have Extreme3d Logitech joystick connected through a chicklet.
I tried to search fo it in the forums but what I found was without any explanation and I quiet didnt understand.
can anyone explain the meaning of using the AND operator..
p3_sw_5=(p3_aux&1<<7)?0:1;
tnx ahead
& is a "bitwise and" operator. Think of it converting the two numbers to binary, then wherever the two numbers both have a "1" in the same place, keeps the "1", otherwise clears it to "0".
7 & 2 returns a 2 (00000111 & 00000010 returns a 00000010)
| is a "bitwise or" operator -- if either bit has a 1 it returns a 1. If both bits are 0 it returns a 0.
^ is a "bitwise exclusive OR" (sometimes known as XOR). Returns a 1 if either number has a 1 but not both, otherwise a 0.
~ is "bitwise NOT" (one's complement). Returns the opposite number (1 to 0, 0 to 1).
Note they all operate on the bits (as in binary numbers).
There used to be more combinations in BASIC's wild days (when Basic was BASIC), but I guess C isn't as wild.
Do not confuse & and &&, and | and ||. The latter symbols apply to the whole number, not to the bits.
<< shifts each bit to the left (sort of like multiplying by 2 each time). >> shifts each bit to the right (dividing by 2 each time). In your case X<<7 shifts all the bits in X to the left by 7: 0000.0001 to 1.0000.0000 (I put in decimal points to make it clearer). You'll have to find out what p3_aux produces in the lowest bit, then all the other bits are cleared, it then shifts it left 7 bits.
X?0:1 I think evaluates X, then returns a 0 if true, or 1 if false. It's a shorthand if statement. So after doing the above, it is evaulated true (1), it returns a zero, else if it is false (0) it returns a one, and is put into p3_sw_5. (a true might be from any non-zero value, and false is always zero.)
I don't know offhand the precidence of which does what first. I am assuming it is ((p3_aux & 1) << 7), not (p3_aux & (1 << 7)), but looking again I can't tell and I don't remember.
Find yourself a book on C -- it's all in there!
Roger.
_______________________________
Always do right! This will gratify some people and astonish the rest. - Mark Twain
jacobhurwitz
18-02-2008, 11:36
I don't know offhand the precidence of which does what first. I am assuming it is ((p3_aux & 1) << 7), not (p3_aux & (1 << 7)), but looking again I can't tell and I don't remember.
The bitshift operator has higher precedence than the binary and operator. So, it would be (p3_aux & (1<<7)).
What does this mean? Digital inputs are either 0 or 1. This would be like p1_sw_trig or something. It is 0 if it's not pressed and 1 if it is. An analog input can go from 0 to 255. An example of this is p1_x, which varies depending on where the joystick is.
p3_aux is an analog input, but not in the sense of p1_x. p3_aux is a combination of 8 digital inputs. The right-most bit refers to one switch, the next bit to another, etc. So p3_aux&1<<7 says, "is the seventh bit from the right true?" (It's actually the eighth bit, because we use zero-based counting here. p3_aux&1<<0 gives you the rightmost, etc.)
Does this make sense?
So p3_aux&1<<7 says, "is the seventh bit from the right true?" In this case p3_aux&1<<7 -- or to my careful mind p3_aux&(1<<7) -- is a roundabout way of saying p3_aux&0x80 , the 0x80 simplifying the 1<<7?
This is why I hesitated when I wrote it with the parentheses. I wasn't sure of precedence in the first place, and after I typed it both ways I realized that what you're doing (1<<7 first, then the &) could be what was intended. Which is why using extra parentheses, even if not legally necessary makes sure you're writing what you indend and others understand!
vBulletin® v3.6.4, Copyright ©2000-2017, Jelsoft Enterprises Ltd.