user4924: I have a code that reads the value of a sensor and because it needs to be very fast it does not use digitalRead but a strange form of reading. In the top of the code it shows:
#define IRSENSOR PIND
int irpin = 2;

I have no idea why this PIND referes to my arduino pin 2, but it only work if my sensor is in pin 2. If I want to change to pin 9 and only change the irpin varialbe it does not work. Why?
To read the sensor I use this code:
while (IRSENSOR & (1 << irpin )) {

Have no idea why this work. Does anyone know this kind of programming with arduino?

user3825: Reason explained by @BrettM. To be more perfect use macros everywhere, e.g.:
#define IRSENSOR PIND
#define IRSENSOR_PIN PD2

while (IRSENSOR & _BV(IRSENSOR_PIN)) {

Arduino's digitalWrite(),digitalRead(),pinMode() functions are useless for more serious coding because they are 1) slow 2) you cannot address pins not connected to headers (e.g. at Arduino2560).

user1585: PIND refers to the arduino's IO register for input in port D
Without arduino, one has to refer to pins by specific bits in 3 different sets of registers for each port (PIN*, DDR*, and PORT*; B,C, and D). 
The arduino code makes it simpler to pick out a single pin by hiding all of this, but it does make reading a pin take much longer.
In the code, the specific pin is being selected by masking off all but the "irpin" bit in PIND, so changing irpin will pick a different bit and thus a different pin.
Pin 9 is actually bit 1 of port B, not D, so see if this works:
#define IRSENSOR PINB
int irpin = 1;

alternatly, using a reference instead of define,
volatile uint8_t& IRSENSOR = PINB;
int irpin = 1;


4924: <p>I have a code that reads the value of a sensor and because it needs to be very fast it does not use digitalRead but a strange form of reading. In the top of the code it shows:</p>

<pre><code>#define IRSENSOR PIND
int irpin = 2;
</code></pre>

<p>I have no idea why this PIND referes to my arduino pin 2, but it only work if my sensor is in pin 2. If I want to change to pin 9 and only change the irpin varialbe it does not work. Why?</p>

<p>To read the sensor I use this code:</p>

<pre><code>while (IRSENSOR &amp; (1 &lt;&lt; irpin )) {
</code></pre>

<p>Have no idea why this work. Does anyone know this kind of programming with arduino?</p>
 3825: <p>Reason explained by @BrettM. To be more perfect use macros everywhere, e.g.:</p>

<pre><code>#define IRSENSOR PIND
#define IRSENSOR_PIN PD2

while (IRSENSOR &amp; _BV(IRSENSOR_PIN)) {
</code></pre>

<p>Arduino's <em>digitalWrite(),digitalRead(),pinMode()</em> functions are useless for more serious coding because they are 1) slow 2) you cannot address pins not connected to headers (e.g. at <em>Arduino2560</em>).</p>
 1585: <p>PIND refers to the arduino's IO register for input in port D</p>

<p>Without arduino, one has to refer to pins by specific bits in 3 different sets of registers for each port (PIN*, DDR*, and PORT*; B,C, and D). </p>

<p>The arduino code makes it simpler to pick out a single pin by hiding all of this, but it does make reading a pin take much longer.</p>

<p>In the code, the specific pin is being selected by masking off all but the "irpin" bit in PIND, so changing irpin will pick a different bit and thus a different pin.</p>

<p>Pin 9 is actually bit 1 of port B, not D, so see if this works:</p>

<pre><code>#define IRSENSOR PINB
int irpin = 1;
</code></pre>

<p>alternatly, using a reference instead of define,</p>

<pre><code>volatile uint8_t&amp; IRSENSOR = PINB;
int irpin = 1;
</code></pre>
