Go to Post Man, I never thought I'd have to cite a rule during the offseason. - Karibou [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
 
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 18-02-2008, 08:58
TEAM1949 TEAM1949 is offline
Registered User
FRC #1949
 
Join Date: Jan 2006
Location: IsRaEl
Posts: 24
TEAM1949 is an unknown quantity at this point
joystick and extra buttons

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

Code:
p3_sw_5=(p3_aux&1<<7)?0:1;
tnx ahead
  #2   Spotlight this post!  
Unread 18-02-2008, 09:48
Roger Roger is offline
Registered User
FRC #1153
 
Join Date: Jan 2006
Rookie Year: 1900
Location: Walpole MA
Posts: 685
Roger has a reputation beyond reputeRoger has a reputation beyond reputeRoger has a reputation beyond reputeRoger has a reputation beyond reputeRoger has a reputation beyond reputeRoger has a reputation beyond reputeRoger has a reputation beyond reputeRoger has a reputation beyond reputeRoger has a reputation beyond reputeRoger has a reputation beyond reputeRoger has a reputation beyond repute
Re: joystick and extra buttons

& 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
  #3   Spotlight this post!  
Unread 18-02-2008, 11:36
jacobhurwitz jacobhurwitz is offline
Registered User
FRC #0449 (Blair Robot Project)
Team Role: Programmer
 
Join Date: Jan 2008
Rookie Year: 2007
Location: Maryland
Posts: 45
jacobhurwitz has a spectacular aura aboutjacobhurwitz has a spectacular aura aboutjacobhurwitz has a spectacular aura about
Re: joystick and extra buttons

Quote:
Originally Posted by Roger View Post
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?
  #4   Spotlight this post!  
Unread 18-02-2008, 13:22
Roger Roger is offline
Registered User
FRC #1153
 
Join Date: Jan 2006
Rookie Year: 1900
Location: Walpole MA
Posts: 685
Roger has a reputation beyond reputeRoger has a reputation beyond reputeRoger has a reputation beyond reputeRoger has a reputation beyond reputeRoger has a reputation beyond reputeRoger has a reputation beyond reputeRoger has a reputation beyond reputeRoger has a reputation beyond reputeRoger has a reputation beyond reputeRoger has a reputation beyond reputeRoger has a reputation beyond repute
Re: joystick and extra buttons

Quote:
Originally Posted by jacobhurwitz
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!
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Buttons to Pins on Joystick yarb65 Electrical 1 24-08-2007 20:39
Programming Extra Joystick Buttons Inverted Programming 2 08-02-2007 11:45
Buttons, Buttons and more Buttons (2006) Freddy Schurr Team Organization 11 14-12-2005 21:36
joystick buttons and a quick question h0x4r Programming 2 17-02-2005 00:18
Using the extra buttons on the joystick. Code\\Pilot Programming 7 04-12-2004 19:30


All times are GMT -5. The time now is 17:31.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi