Pages: [1]   Go Down
Author Topic: bit.h set of bitmasks defines compementary to binary.h  (Read 898 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 169
Posts: 12441
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley

Only BIT0 - BIT5 tested,
Code:
//
//    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
Logged

Rob Tillaart

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

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 106
Posts: 6373
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if ((DDRB & BIT4) == BIT4)
To keep consistent with Atmel conventions, you should probably say:
Code:
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...)
Logged

Offline Offline
Edison Member
*
Karma: 17
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Why not just
Code:
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:
#define _BV(bit) (1 << (bit))

from avr/sfr_defs.h
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 64
Posts: 6893
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I like these BITn, easy to read and use.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Code:
if ((DDRB & DDB4) == DDB4)

Why not just
Code:
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.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 169
Posts: 12441
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-wink
Logged

Rob Tillaart

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

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 64
Posts: 6893
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Pages: [1]   Go Up
Jump to: