Re: Better bitmask macro

Discussion in 'C Programming' started by Tim Rentsch, Mar 28, 2012.

  1. Tim Rentsch

    Tim Rentsch Guest

    Guillaume Dargaud <> writes:

    > Hello all,
    > I wrote this small macro to provide a bit mask from bit L to bit H:
    >
    > // Does a bit mask from bit L to bit H inclusive on 64 bits.
    > // Warning H must be <=62 (not 63) and L<=H
    > // Example: BITMASK(5,3)=0x1C
    > #define BITMASK(H,L) ( ((1UL<<((H)-(L)+1))-1)<<(L) )
    >
    > The problem is that H must not be 63.
    > Is there a better way to write it so that it works with all bits ?
    >
    > I can think of adding (H)==63 ? ... : BITMASK(H,L)
    > but there's probably a better way.


    #define BITMASK(H,L) \
    ( (0xFFFFFFFFFFFFFFFF >> (H) << (H) ^ 0xFFFFFFFFFFFFFFFF) >> (L) << (L) )

    This definition works for implementations that have a 64-bit (or
    greater) integer type, even if they don't have unsigned long long.
    Tim Rentsch, Mar 28, 2012
    #1
    1. Advertising

  2. Tim Rentsch

    Tim Rentsch Guest

    Tim Rentsch <> writes:

    > Guillaume Dargaud <> writes:
    >
    >> Hello all,
    >> I wrote this small macro to provide a bit mask from bit L to bit H:
    >>
    >> // Does a bit mask from bit L to bit H inclusive on 64 bits.
    >> // Warning H must be <=62 (not 63) and L<=H
    >> // Example: BITMASK(5,3)=0x1C
    >> #define BITMASK(H,L) ( ((1UL<<((H)-(L)+1))-1)<<(L) )
    >>
    >> The problem is that H must not be 63.
    >> Is there a better way to write it so that it works with all bits ?
    >>
    >> I can think of adding (H)==63 ? ... : BITMASK(H,L)
    >> but there's probably a better way.

    >
    > #define BITMASK(H,L) \
    > ( (0xFFFFFFFFFFFFFFFF >> (H) << (H) ^ 0xFFFFFFFFFFFFFFFF) >> (L) << (L) )
    >
    > This definition works for implementations that have a 64-bit (or
    > greater) integer type, even if they don't have unsigned long long.


    Urk. Off-by-one error.

    #define BITMASK(H,L) \
    ( (0xFFFFFFFFFFFFFFFF >> (H) >> 1 << (H) << 1 ^ 0xFFFFFFFFFFFFFFFF) >> (L) << (L) )
    Tim Rentsch, Mar 29, 2012
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rod Nibbe

    Bitmask & Graphics Question

    Rod Nibbe, Oct 26, 2004, in forum: Java
    Replies:
    2
    Views:
    1,612
    Rod Nibbe
    Oct 26, 2004
  2. Andrey Brozhko
    Replies:
    1
    Views:
    1,071
    Andrey Brozhko
    Dec 10, 2004
  3. =?iso-8859-9?Q?Tongu=E7?= Yumruk

    Bitmask representation of integers

    =?iso-8859-9?Q?Tongu=E7?= Yumruk, Oct 8, 2003, in forum: Python
    Replies:
    3
    Views:
    858
    Scott David Daniels
    Oct 8, 2003
  4. Xah Lee
    Replies:
    0
    Views:
    319
    Xah Lee
    Apr 23, 2007
  5. Eric Sosman

    Re: Better bitmask macro

    Eric Sosman, Mar 28, 2012, in forum: C Programming
    Replies:
    3
    Views:
    342
    Francois Grieu
    Mar 29, 2012
Loading...

Share This Page