Basically anything besides 000 is a TRUE isn't it?
Yes.
This is a basic bit test.
Code:
// A few simple bit tasks for u-controllers...
// Test bit
(1<<bit) & x
// Set bit
x |= (1<<bit);
// Set multiple bits
x |= (1<<bit1) | (1<<bit2) | (1<<bit3);
// Clear bit
x &= ~(1<<bit);
// Clear multiple bits
x &= ~((1<<bit1) | (1<<bit2) | (1<<bit3));
An easier example would be if I had a microcontroller with an 8-bit port, PORTA.
On the input for b3 (usually corresponds to the forth pin, because b0 is the first pin) I have a switch that pulls the pin to logic level high and we want a motor to start...
Code:
if ((1<<3) & PORTA)
StartMotor();
In the AVR world you see a lot of the "<<" notation to set and clear bits. I tend to do it with all different microcontroller family projects now days...
This is so that you can see each individual bit.
Code:
x = (1<<0) | //comment
(1<<1) | //comment
(0<<2) ; //comment
You can see that the 0<< is kept in, just so a comment can be made as to why is was 0.
This is from a PIC, setting up the oscillator - Note that 0b is a non-standard but widely used way of writing binary literal and the _xx_POSITION is a defined number (0-7) in a header file for the XC8 compiler.
Code:
OSCCON = (0b1110 << _OSCCON_IRCF_POSITION) | // 8 MHz or 32 MHz HF(see Section 5.2.2.1 ?HFINTOSC?)
(0b00 << _OSCCON_SCS_POSITION) | // Clock determined by FOSC<2:0> in Configuration Word 1
(0 << _OSCCON_SPLLEN_POSITION); // 4x PLL Is enabled