[Wiltshire] Random C coding question...
RobertCL at iname.com
Tue Apr 22 13:50:47 BST 2008
Fairly random question, kind of related to linux, I'm hoping someone on
here might be able to answer as it is C-related (and lots of linux things
are C) and I can't think how to google for the answer or where else to
ask. (putting "1<<" into google isn't helpful!)
Can someone explain to me the significance of 1<<PB1 in the following
// 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.
The code extract is from some AVR code I'm playing with and I know what
the end result of the code is - the LED connected to pin 1 of port B on
the atmega168 chip turns on or off. And I can modify the code to turn
stuff on and off on different ports by changing (eg) PB1 to PB2 and PORTB
to PORTD, etc, etc and it works. I just don't understand how it works!
My initial thoughts were some kind of bit mask, but the "1" just doesn't
make sense to me. I'm guessing that PORTB is an 8-bit int with each bit
representing a pin, and that or-ing / and-ing (1<<PB1) somehow toggles the
bit for pin1. But I could be waaay off with that guess.
Anyway, I could go on all day about the things I don't know about C, so
I'll leave it there..
More information about the Wiltshire