comp.lang.ada
 help / color / mirror / Atom feed
From: robin.vowels@gmail.com
Subject: Re: Efficient Bit Vector Manipulation.
Date: Sun, 17 May 2015 06:55:06 -0700 (PDT)
Date: 2015-05-17T06:55:06-07:00	[thread overview]
Message-ID: <06cf4bbd-b063-44ac-ba49-0f479b4a7b1e@googlegroups.com> (raw)
In-Reply-To: <62605fe5-6ecf-4750-b13c-24bce65e3439@googlegroups.com>

On Friday, May 15, 2015 at 10:07:41 PM UTC+10, 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. Then, we call k = length( I ) - 1. k is the number of useful bits, i.e. the bits following the first 1.
> 
> For instance : 
>  - I = 1 is represented by 1 and k = 0.
>  - I = 2 is represented by 10 and k = 1.
>  - I = 3 is represented by 11 and k = 1.
>  - I = 4 is represented by 100 and k = 2. 
> 
> Then I would like to :
> - extract the b = floor(k/2) bits leading the first 1 starting from MSB and convert them to an integer : B
> - extract the remaining e = Ceiling (k/2) bits and convert them to an integer : E.
> 
> then I = 2^k + B * 2^b + E
> 
> How can I do that efficiently in Ada ? I mean I thought of using boolean arrays but will this
> be optimized by the compiler to CPU instructions ? I used to know how to do it in assembly langage on the Intel 386 but that seems not portable at all :-). Maybe I should use shifts. 
> 
> Any suggestion ?

In PL/I there are specific functions for extracting bits from an integer.
These include IAND (and there are others, probably not relevent here).
Shifting is performed by ISRL and ISLL.
All of these operations (and others) have direct CPU instructions.

  parent reply	other threads:[~2015-05-17 13:55 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
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 [this message]
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