user34045: I'm trying to record the state of some pins over time. To save memory, I'm thinking about encoding the pin states into a single integer, like this:
Pin 1: TRUE
Pin 2: FALSE
Pin 3: FALSE
Pin 4: TRUE

Pin 1 = 1, Pin 2 = 2, Pin 3 = 4, Pin 4 = 8
1 + 0 + 0 + 8
Result: 9

This is pretty efficient.
But how do I decode the pin states from this best?
Or do you recommend a better way to do this?

user4143: You are almost there with that. Personally I'd use OR not +:
This is a better way of defining the pins instead of using 1, 2, 4 etc. It results in the same thing, but you can see the bit number in there. The << is left shift.
#define PIN0 (1 << 0)
#define PIN1 (1 << 1)
#define PIN2 (1 << 2)
#define PIN3 (1 << 4)

Now you can combine:
uint8_t val = PIN0 | PIN3; // 9

And you can check if one is set:
if (val & PIN3) {
    // ....
}

To loop through the pins you can use the << again:
for (uint8_t pin = 0; pin < 8; pin++) {
    if (val & (1 << pin)) {
        // ....
    }
}

etc.

34045: <p>I'm trying to record the state of some pins over time. To save memory, I'm thinking about encoding the pin states into a single integer, like this:</p>

<pre><code>Pin 1: TRUE
Pin 2: FALSE
Pin 3: FALSE
Pin 4: TRUE

Pin 1 = 1, Pin 2 = 2, Pin 3 = 4, Pin 4 = 8
1 + 0 + 0 + 8
Result: 9
</code></pre>

<p>This is pretty efficient.
But how do I decode the pin states from this best?
Or do you recommend a better way to do this?</p>
 4143: <p>You are almost there with that. Personally I'd use OR not +:</p>

<p>This is a better way of defining the pins instead of using 1, 2, 4 etc. It results in the same thing, but you can see the <em>bit number</em> in there. The <code>&lt;&lt;</code> is <em>left shift</em>.</p>

<pre><code>#define PIN0 (1 &lt;&lt; 0)
#define PIN1 (1 &lt;&lt; 1)
#define PIN2 (1 &lt;&lt; 2)
#define PIN3 (1 &lt;&lt; 4)
</code></pre>

<p>Now you can combine:</p>

<pre><code>uint8_t val = PIN0 | PIN3; // 9
</code></pre>

<p>And you can check if one is set:</p>

<pre><code>if (val &amp; PIN3) {
    // ....
}
</code></pre>

<p>To loop through the pins you can use the <code>&lt;&lt;</code> again:</p>

<pre><code>for (uint8_t pin = 0; pin &lt; 8; pin++) {
    if (val &amp; (1 &lt;&lt; pin)) {
        // ....
    }
}
</code></pre>

<p>etc.</p>
