Reading digital input of Arduino

Hey guys

Simple question, but I'm trying to get my Arduino uno to read the digital inputs of 4 pins (2, 3, 4, 5), then add the binary values together into a single integer.

Using DigitalRead on the pins is fine, but what's the least messy way to define each pin as a binary value, then add those values together?

Cheers

So... The data represents a binary number, right? 0000 (0 decimal) to 1111 (15 decimal) right?

Simply multiply each bit value by its "bit weight" and sum.

Bit 0 x 1 (The least significant or rightmost bit).
Bit 1 x 2
Bit 2 x 4
Bit 3 x 8

For example, 0101:
1 x 1 = 1
0 x 2 = 0
1 x 4 = 4
0 x 8 = 0

Sum = 5

Or, you can use bitWrite() to write those bits into an integer variable and that integer will display in decimal by default. (Initialize the variable to zero first to make sure the more-significant bits are zero.) This is how I'd do it, but do whatever seems the most logical to you.

If you wish, you can use a type byte (8-bits) instead of an integer (16-bits).

FYI - The Windows calculator in Programmer view will do binary to decimal conversion. (In case you want to check your results.)