Velocity Reviews > Re: Better bitmask macro

# Re: Better bitmask macro

Eric Sosman
Guest
Posts: n/a

 03-28-2012
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
http://www.velocityreviews.com/forums/(E-Mail Removed)d

BartC
Guest
Posts: n/a

 03-28-2012

"Eric Sosman" <(E-Mail Removed)> wrote in message
news:jkuv47\$ohj\$(E-Mail Removed)...
> 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

Eric Sosman
Guest
Posts: n/a

 03-29-2012
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
(E-Mail Removed)d

Francois Grieu
Guest
Posts: n/a

 03-29-2012
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

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Tim Rentsch C Programming 1 03-29-2012 12:31 AM Xah Lee Java 0 04-23-2007 10:53 AM Andrey Brozhko XML 1 12-10-2004 05:12 PM Rod Nibbe Java 2 10-26-2004 03:56 PM =?iso-8859-9?Q?Tongu=E7?= Yumruk Python 3 10-08-2003 02:50 PM

Advertisments