comp.lang.ada
 help / color / mirror / Atom feed
From: Vincent <vincent.diemunsch@gmail.com>
Subject: Efficient Bit Vector Manipulation.
Date: Fri, 15 May 2015 05:07:40 -0700 (PDT)
Date: 2015-05-15T05:07:40-07:00	[thread overview]
Message-ID: <62605fe5-6ecf-4750-b13c-24bce65e3439@googlegroups.com> (raw)

 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 ?

Kind regards,

Vincent

             reply	other threads:[~2015-05-15 12:07 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-15 12:07 Vincent [this message]
2015-05-15 12:48 ` Efficient Bit Vector Manipulation 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
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