From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.182.108.196 with SMTP id hm4mr27211605obb.33.1431870907270; Sun, 17 May 2015 06:55:07 -0700 (PDT) X-Received: by 10.50.65.38 with SMTP id u6mr409769igs.1.1431870907239; Sun, 17 May 2015 06:55:07 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!feeder.eternal-september.org!news.glorb.com!h15no4459962igd.0!news-out.google.com!kd3ni19923igb.0!nntp.google.com!h15no4459957igd.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Sun, 17 May 2015 06:55:06 -0700 (PDT) In-Reply-To: <62605fe5-6ecf-4750-b13c-24bce65e3439@googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=123.2.70.40; posting-account=S_MdrwoAAAD7T2pxG2e393dk6y0tc0Le NNTP-Posting-Host: 123.2.70.40 References: <62605fe5-6ecf-4750-b13c-24bce65e3439@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <06cf4bbd-b063-44ac-ba49-0f479b4a7b1e@googlegroups.com> Subject: Re: Efficient Bit Vector Manipulation. From: robin.vowels@gmail.com Injection-Date: Sun, 17 May 2015 13:55:07 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Xref: news.eternal-september.org comp.lang.ada:25896 Date: 2015-05-17T06:55:06-07:00 List-Id: On Friday, May 15, 2015 at 10:07:41 PM UTC+10, Vincent wrote: > Hello Ada language experts, >=20 > I need to extract in a very efficient way, information from the bit seque= nce that represents a 32 bits Positive number, named=A0I > 0.=20 >=20 > What i would like to do is : >=20 > - find the length of the bit sequence, i.e. the number of bits a given va= lue of I needs to be represented properly as a binary number. This can be c= omputed by taking the first non zero bit starting from the MSB of I. Then, = we call k =3D length( I ) - 1. k is the number of useful bits, i.e. the bit= s following the first 1. >=20 > For instance :=20 > - I =3D 1 is represented by 1 and k =3D 0. > - I =3D 2 is represented by 10 and k =3D 1. > - I =3D 3 is represented by 11 and k =3D 1. > - I =3D 4 is represented by 100 and k =3D 2.=20 >=20 > Then I would like to : > - extract the b =3D floor(k/2) bits leading the first 1 starting from MSB= and convert them to an integer : B > - extract the remaining e =3D Ceiling (k/2) bits and convert them to an i= nteger :=A0E. >=20 > then I =3D 2^k + B * 2^b + E >=20 > 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 a= ll :-). Maybe I should use shifts.=20 >=20 > 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.