comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Efficient Bit Vector Manipulation.
Date: Fri, 15 May 2015 19:26:02 +0300
Date: 2015-05-15T19:26:02+03:00	[thread overview]
Message-ID: <crml0kFpmvdU1@mid.individual.net> (raw)
In-Reply-To: <62605fe5-6ecf-4750-b13c-24bce65e3439@googlegroups.com>

On 15-05-15 15:07 , Vincent wrote:
> Hello Ada language experts,
>
> I need to extract in a very efficient way, information from the bit
> sequence that represents a 32 bits Positive number, named I > 0.
>
> What i would like to do is :
>
> - find the length of the bit sequence, i.e. the number of bits a
> given value of I needs to be represented properly as a binary number.
> This can be computed by taking the first non zero bit starting from
> the MSB of I.

Very well-known problem... see

http://en.wikipedia.org/wiki/Find_first_set

and

http://graphics.stanford.edu/~seander/bithacks.html

The latter page gives C code, which should be easy to translate to Ada 
using the modular types from Interfaces and their shift operations.

Same suggestion for the rest of your questions: use modular types, 
shifts and masks.

I doubt that any compiler provides good optimization of such operations 
on Boolean arrays, even if packed to one bit per element. Anyway, the 
Ada RM does not define in which order the bits are indexed, so code 
using Boolean arrays would be unportable if it uses Unchecked_Conversion 
to converts between integers and arrays.

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .


  parent reply	other threads:[~2015-05-15 16:26 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-15 12:07 Efficient Bit Vector Manipulation Vincent
2015-05-15 12:48 ` Colin Paul de Gloucester
2015-05-16 17:12   ` Dennis Lee Bieber
2015-05-15 16:26 ` Niklas Holsti [this message]
2015-05-16 11:58   ` vincent.diemunsch
2015-05-15 16:58 ` Jeffrey R. Carter
2015-05-16 12:06   ` vincent.diemunsch
2015-05-17 13:55 ` robin.vowels
2015-05-18  7:53 ` Stefan.Lucks
2015-05-18 11:43   ` vincent.diemunsch
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox