Re: Better bitmask macro

Discussion in 'C Programming' started by Eric Sosman, Mar 28, 2012.

  1. Eric Sosman

    Eric Sosman Guest

    On 3/28/2012 4:25 AM, Guillaume Dargaud wrote:
    > 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.


    Untested:

    #define BITMASK(H,L) ( (~0ULL >> (63 - (H)) ^ \
    (~0ULL >> (63 - (L)) >> 1 )

    --
    Eric Sosman
    d
     
    Eric Sosman, Mar 28, 2012
    #1
    1. Advertising

  2. Eric Sosman

    BartC Guest

    "Eric Sosman" <> wrote in message
    news:jkuv47$ohj$...
    > On 3/28/2012 4:25 AM, Guillaume Dargaud wrote:
    >> 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.

    >
    > Untested:
    >
    > #define BITMASK(H,L) ( (~0ULL >> (63 - (H)) ^ \
    > (~0ULL >> (63 - (L)) >> 1 )


    It needs the parentheses taken care of:

    #define BITMASK(H,L) (~0ULL >> (63 - (H))) ^ (~0ULL >> (63 - (L)) >> 1 )

    --
    Bartc
     
    BartC, Mar 28, 2012
    #2
    1. Advertising

  3. Eric Sosman

    Eric Sosman Guest

    On 3/28/2012 11:35 AM, Guillaume Dargaud wrote:
    >> #define BITMASK(H,L) (~0ULL>> (63 - (H))) ^ (~0ULL>> (63 - (L))>> 1 )

    >
    > Neat, thanks. A bit beyond my understanding !


    The first piece produces a mask with 1 bits from position H
    downward: With H==6 you get 00...001111111. The second piece has
    two steps: It produces a mask with 1 bits from position L and below,
    then right-shifts to get a mask with 1's from (L-1) and downward.
    Then we XOR the pieces to invert (i.e., clear) those low bits out of
    the first mask, and what remains is the middle span you wanted.

    > It still needs parentheses around the whole thing, though:
    > #define BITMASK(H,L) ( (~0ULL>> (63 - (H))) ^ (~0ULL>> (63 - (L))>> 1 ) )


    Sorry about the parentheses, but I can take comfort in the fact
    that their abundance confused more folks than just me! Besides, I
    *said* it was untested, didn't I?

    --
    Eric Sosman
    d
     
    Eric Sosman, Mar 29, 2012
    #3
  4. On 29/03/2012 03:22, Eric Sosman wrote:
    > On 3/28/2012 11:35 AM, Guillaume Dargaud wrote:
    >>> #define BITMASK(H,L) (~0ULL>> (63 - (H))) ^ (~0ULL>> (63 - (L))>> 1 )

    >>
    >> Neat, thanks. A bit beyond my understanding !

    >
    > The first piece produces a mask with 1 bits from position H
    > downward: With H==6 you get 00...001111111. The second piece has
    > two steps: It produces a mask with 1 bits from position L and below,
    > then right-shifts to get a mask with 1's from (L-1) and downward.
    > Then we XOR the pieces to invert (i.e., clear) those low bits out of
    > the first mask, and what remains is the middle span you wanted.
    >
    >> It still needs parentheses around the whole thing, though:
    >> #define BITMASK(H,L) ( (~0ULL>> (63 - (H))) ^ (~0ULL>> (63 - (L))>> 1 ) )

    >
    > Sorry about the parentheses, but I can take comfort in the fact
    > that their abundance confused more folks than just me! Besides, I
    > *said* it was untested, didn't I?


    Why not

    #define BITMASK(H,L) ( (2ULL<<(H))-(1ULL<<(L)) )


    Francois Grieu
     
    Francois Grieu, Mar 29, 2012
    #4
    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,663
    Rod Nibbe
    Oct 26, 2004
  2. Andrey Brozhko
    Replies:
    1
    Views:
    1,151
    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:
    918
    Scott David Daniels
    Oct 8, 2003
  4. Xah Lee
    Replies:
    0
    Views:
    369
    Xah Lee
    Apr 23, 2007
  5. Tim Rentsch

    Re: Better bitmask macro

    Tim Rentsch, Mar 28, 2012, in forum: C Programming
    Replies:
    1
    Views:
    437
    Tim Rentsch
    Mar 29, 2012
Loading...

Share This Page