Chagrin:
See Arduino Playground - BitMathvoid loop() {
val = 0; // initialize to zero
if( digitalRead(onepin)== HIGH)
val &= B00000001;
if (digitalRead(twopin) == HIGH)
val &= B00000010;
if (digitalRead(fourpin) == HIGH)
val &= B00000100;
delay(100);
Serial.println(val);
}
That seems to be clearing the other bits rather than setting the bit you wanted. In any case I'd suggest putting the pin numbers in an array and using a for loop to read from each pin and write the result to the corresponding bit using bitWrite().