Go Down

Topic: bit.h set of bitmasks defines compementary to binary.h (Read 977 times) previous topic - next topic

robtillaart

needed some bitmasks to keep code readable  e.g. if ((DDRB & BIT4) == BIT4)

first wanted to call them B0 B1 etc but then I had a conflict with Binary.h in the core (especially the values B10 and B11 :)

Only BIT0 - BIT5 tested,
Code: [Select]

//
//    FILE: bit.h
//  AUTHOR: Rob Tillaart
//    DATE: 2012-03-30
//
// PUPROSE: simple bitmasks
//
#ifndef BIT_H
#define BIT_H

#define BIT0 1
#define BIT1 (1<<1)
#define BIT2 (1<<2)
#define BIT3 (1<<3)
#define BIT4 (1<<4)
#define BIT5 (1<<5)
#define BIT6 (1<<6)
#define BIT7 (1<<7)
#define BIT8 (1<<8)
#define BIT9 (1<<9)
#define BIT10 (1<<10)
#define BIT11 (1<<11)
#define BIT12 (1<<12)
#define BIT13 (1<<13)
#define BIT14 (1<<14)
#define BIT15 (1<<15)
#define BIT16 (1<<16)
#define BIT17 (1<<17)
#define BIT18 (1<<18)
#define BIT19 (1<<19)
#define BIT20 (1<<20)
#define BIT21 (1<<21)
#define BIT22 (1<<22)
#define BIT23 (1<<23)
#define BIT24 (1<<24)
#define BIT25 (1<<25)
#define BIT26 (1<<26)
#define BIT27 (1<<27)
#define BIT28 (1<<28)
#define BIT29 (1<<29)
#define BIT30 (1<<30)
#define BIT31 (1<<31)

#endif
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

westfw

Code: [Select]
if ((DDRB & BIT4) == BIT4)
To keep consistent with Atmel conventions, you should probably say:
Code: [Select]
if ((DDRB & DDB4) == DDB4)
(although personally, I rather dislike having the same bit have different names depending on which register it's in, especially when you get things like multiple identical uarts and are supposed to use RXC0 or RXC1 as if they had different values.  Sigh.)
(On the plus side, Atmel does seem to have named every bit, and if you stick to their definitions, your code with theoretically be more understandable by everyone...)

WizenedEE


Code: [Select]
if ((DDRB & BIT4) == BIT4)
To keep consistent with Atmel conventions, you should probably say:
Code: [Select]
if ((DDRB & DDB4) == DDB4)


Why not just
Code: [Select]

if (DDRB & DDB4)


It's either DDB4 (which is not 0; ie true) or 0 (false)

There's also the  _BV(x) macro. So your BIT1 would become _BV(1)
Code: [Select]

#define _BV(bit) (1 << (bit))


from avr/sfr_defs.h

liudr


Makuna



Code: [Select]
if ((DDRB & DDB4) == DDB4)

Why not just
Code: [Select]

if (DDRB & DDB4)


It's either DDB4 (which is not 0; ie true) or 0 (false)



You follow the model of "(flag & mask) == mask" to future proof your code incase the mask becomes more than one bit.

robtillaart

Quote
#define _BV(bit) (1 << (bit))
from avr/sfr_defs.h


didn't know that one , thanks

Quote
Why not just
if (DDRB & DDB4)

you are 100% correct, and the (gcc) compiler optimizes the code to what you proposed, but the main reason is to be explicit and future proof as Makuna pointed out.

future proof, I like the term, it's like backwards compatibility in advance ;)
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

liudr

I thought future proof means paying more for your electronics at big box shops like the only one man standing: Best Buy in US and then you are promised to be able to sell your electronics back at a "good" price when you upgrade to a new one, again from their shop.

Go Up