Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
02/26/09 10:22
Read: times


 
#162877 - Yes, but...
Responding to: ???'s previous message
David Prentice said:
You normally want to use binary constants for things like setting a particular bit in a special function register. So you may:

#define LED_RED 3
#define LED_GREEN 0
#define LED_BLUE 1
variable = (1<<LED_RED) | (1<<LED_GREEN) | (1<<LED_BLUE);   // RGB lit
variable = (1<<LED_RED) | (0<<LED_GREEN) | (1<<LED_BLUE);   // R_B lit

 

Agreed, but that always looks really cumbersome & ugly to me.

I would tend, rather, to do something like:
// Bit positions - 0 is LSB
#define LED_RED_BIT_NUM   3
#define LED_GREEN_BIT_NUM 0
#define LED_BLUE_BIT_NUM  1

// Bit values
#define LED_RED_BIT    (1<<LED_RED_BIT_NUM)
#define LED_GREEN_BIT  (1<<LED_GREEN_BIT_NUM)
#define LED_BLUE_BIT   (1<<LED_BLUE_BIT_NUM)

variable = LED_RED_BIT | LED_GREEN_BIT | LED_BLUE_BIT;   // RGB lit
variable = LED_RED_BIT |                 LED_BLUE_BIT;   // R_B lit

 

Just my preference, of course!

For this specific case, of course, you could make definitions like LED_RED_ON and LED_BLUE_OFF...

List of 15 messages in thread
TopicAuthorDate
Bin in C            01/01/70 00:00      
   Possible solution            01/01/70 00:00      
      You need to check if this works with your compiler            01/01/70 00:00      
         Does it work with any?            01/01/70 00:00      
            popular demand            01/01/70 00:00      
   My compiler is IAR            01/01/70 00:00      
      because that's a non-standard (non-ANSI) extension            01/01/70 00:00      
      What made you beleive that it would work?            01/01/70 00:00      
   Use #define names            01/01/70 00:00      
      Yes, but...            01/01/70 00:00      
   Use macros for that.            01/01/70 00:00      
   facilitates my understanding            01/01/70 00:00      
      Working with flags            01/01/70 00:00      
         how I do it            01/01/70 00:00      
   I use a macro            01/01/70 00:00      

Back to Subject List