Convert Decimal to Binary and write to pins

New Arduino user here.

Theres plenty of tutorials on how to do dec to binary converion to drive LEDa via a shift register, but fewer on how to do dec to binary and drive 8 pins direct.

There`s several ways to do this.

I`ve used this method http://luckylarry.co.uk/programming-tutorials/arduino-programming/arduino-convert-decimal-to-binary/ and it works fine.

But this method

{
    int var = get_value_between_zero_and_six();
    digitalWrite(11, HIGH && (var & B00001000));
    digitalWrite(12, HIGH && (var & B00000100));
    digitalWrite(13, HIGH && (var & B00000010));
    digitalWrite(14, HIGH && (var & B00000001));
}

from the the old forum looks a lot neater, but I`m having trouble understanding how it works.

digitalwrite can be HIGH or LOW
B0000001 is a bitmask which is used with var, I understand that bit.

So, only digitalwrite if HIGH and (result of var and bitmask) is true?

Thanks.

this should work too

{
    int var = get_value_between_zero_and_six();
    digitalWrite(11, var & B00001000);
    digitalWrite(12, var & B00000100);
    digitalWrite(13, var & B00000010);
    digitalWrite(14, var & B00000001);
}

The explanation how it works is done here - http://arduino.cc/it/Reference/BitwiseAnd -

in short the & (bitwise and) compares variables bit by bit and only if both bits (same position) are 1 the resulting value wil have this bit set.

DigitalWrite will write an HIGH if the value is not 0; so if there is one bit set it will be HIGH else LOW(0).

Your example and explanation is much clearer to me.

Thanks.