comp.lang.ada
 help / color / mirror / Atom feed
* Efficient Bit Vector Manipulation.
@ 2015-05-15 12:07 Vincent
  2015-05-15 12:48 ` Colin Paul de Gloucester
                   ` (4 more replies)
  0 siblings, 5 replies; 10+ messages in thread
From: Vincent @ 2015-05-15 12:07 UTC (permalink / 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

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2015-05-18 11:43 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2015-05-18  7:53 ` Stefan.Lucks
2015-05-18 11:43   ` vincent.diemunsch

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