Thread: Advantages of using macro for BIT positions

  1. #1
    Registered User
    Join Date
    Jul 2008
    Location
    Cambridge, UK
    Posts
    5

    Advantages of using macro for BIT positions

    Hello All,
    Are there any advantages of using a macro for bit positions over using a simple bit mask?
    The only ones I can think of are:
    o not having magic numbers throughout your code.
    o readability.

    Are there any others?

    E.G.
    Code:
    #define BIT3    (0x1 <<3)
    ...
    uiData |= BIT3;
    instead of using a simple bit mask:

    Code:
    uiData |= 0x08;

    Regards,
    Don

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, I don't see any DISADVANTAGE with it - it's easier to read "BIT3" than figure out that 8 is bit number three. And any decent compiler should be able to translate 1 << 3 into 8 at compile-time.

    More importantly, you may not want to call it "BIT3", but "EnableSoundBit" or some such, that indicates the purpose of setting this bit.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Location
    Cambridge, UK
    Posts
    5
    Hi Mats,
    Cheers for the fast response.

    More importantly, you may not want to call it "BIT3", but "EnableSoundBit" or some such, that indicates the purpose of setting this bit.
    I want to use a set of macros for generic bit setting, hence the use of "BIT3". Would I be better off with a "BIT(x)" macro?

    Don

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by donmorr View Post
    I want to use a set of macros for generic bit setting, hence the use of "BIT3". Would I be better off with a "BIT(x)" macro?

    Don
    But why would you want to generically set bit X? Normally when you want to set individual bits in a some integer type, it's because they mean something. Or are you planning to do something like a bitmap to keep track of (say) which squares on a chessboard have pieces on them?

    Yes, you can have BIT(x) which translates to (1 << (x)) [don't forget those parenthesis, as << is one of the nasty ones for precedence, so you want to make sure you get what you expect].

    Of coruse, you can then use
    #define EnableSoundBit BIT(3)


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Location
    Cambridge, UK
    Posts
    5
    But why would you want to generically set bit X?
    I work in embedded, so the ability to maniputale a particular 'generic' bit is quite useful. I would like to have a standard approach for doing this within our project.

    don't forget those parenthesis..
    Ahh, cheers for pointing that out :-)


    Don

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    _PARTICULARLY_ in the embedded world, bits usually have particular meaning for each position (or set of positions, e.g. a set of three consecutive bits are used to indicate some 0..7 value).

    Having a macro to define bits (and sets of bits?) would be handy, but you also should seriously consider naming bits once you get to defining the hardware device or software that uses those bit positions, so that you can easily figure out what the code intends to do.

    x |= 8 or x |= 1 << 3 or x |= BIT(3) are all pretty much equally unreadable compared to x |= EnableSoundBit.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Jul 2008
    Location
    Cambridge, UK
    Posts
    5
    but you also should seriously consider naming bits once you get to defining the hardware device or software that uses those bit positions, so that you can easily figure out what the code intends to do.
    Yes I agree, in a situation where I would want to control a particular bit in a hardware register I would always give it a meaningfull name:
    e.g.
    Code:
    #define ADF7020_SREAD               (PORTAbits.RA5)

    But in the case of manipulating data (extracting information, etc), I would still want to use the BIT macros.


    Don

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by donmorr View Post
    Yes I agree, in a situation where I would want to control a particular bit in a hardware register I would always give it a meaningfull name:
    e.g.
    Code:
    #define ADF7020_SREAD               (PORTAbits.RA5)

    But in the case of manipulating data (extracting information, etc), I would still want to use the BIT macros.


    Don
    I still think that you don't use BIT(3) when extracting data - your bit actually means something. Sometimes you want BIT(x), where x is variable, for example with some compression algorithms. But that is a slightly different matter, and in this case, I personally prefer (1 << x) than a BIT(x) macro.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Jul 2008
    Location
    Cambridge, UK
    Posts
    5
    OK, I understand what you mean.
    Cheers for the discussion :-)


    Don

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  4. counting array positions
    By Cpro in forum C++ Programming
    Replies: 4
    Last Post: 02-04-2008, 11:25 PM
  5. L macro
    By George2 in forum C Programming
    Replies: 1
    Last Post: 08-20-2007, 09:24 AM