[Wiltshire] Random C coding question...

Pete Houston ph1 at openstrike.co.uk
Tue Apr 22 14:09:23 BST 2008


Hello Robert,

On Tue, Apr 22, 2008 at 01:50:35PM +0100, Robert wrote:
> 
>      // gerneral purpose LED
>      /* set output to Vcc, red LED off */
>      #define LED1OFF PORTB|=(1<<PB1)
>      /* set output to GND, red LED on */
>      #define LED1ON PORTB&=~(1<<PB1)
> 
> What is it doing?  Why "1" and what does << do / mean / what is going on.
> 
> A bit of background may be required I guess, I'm not a C programmer, but I
> do recall from a C++ book I once read things like 'cout << "some text"',
> so I'm guessing it's some kind of redirect or pipe type thing.

Nice guess, but you're on the wrong path there, I think. << is the
bitwise left-shift operator (and similarly >> is the bitwise right-shift
operator). So, the expresstion 1<<PB1 can be read as "shift the number 1
to the left by PB1 bits", which makes sense if you're working with
something where only a single bit needs to be "on".

HTH,

Pete
-- 
Openstrike - improving business through open source
http://www.openstrike.co.uk/ or call 01722 770036 / 07092 020107
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://mailman.lug.org.uk/pipermail/wiltshire/attachments/20080422/2a74e160/attachment.bin


More information about the Wiltshire mailing list