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.32.73 with SMTP id g9mr13018872obi.0.1431691660347; Fri, 15 May 2015 05:07:40 -0700 (PDT) X-Received: by 10.140.98.175 with SMTP id o44mr4081qge.33.1431691660320; Fri, 15 May 2015 05:07:40 -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!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!h15no2993331igd.0!news-out.google.com!k20ni13808qgd.0!nntp.google.com!z60no596464qgd.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Fri, 15 May 2015 05:07:40 -0700 (PDT) Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=87.91.37.131; posting-account=hya6vwoAAADTA0O27Aq3u6Su3lQKpSMz NNTP-Posting-Host: 87.91.37.131 User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <62605fe5-6ecf-4750-b13c-24bce65e3439@googlegroups.com> Subject: Efficient Bit Vector Manipulation. From: Vincent Injection-Date: Fri, 15 May 2015 12:07:40 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Xref: news.eternal-september.org comp.lang.ada:25883 Date: 2015-05-15T05:07:40-07:00 List-Id: Hello Ada language experts, I need to extract in a very efficient way, information from the bit sequenc= e that represents a 32 bits Positive number, named=A0I > 0.=20 What i would like to do is : - find the length of the bit sequence, i.e. the number of bits a given valu= e of I needs to be represented properly as a binary number. This can be com= puted 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 bits = following the first 1. 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 Then I would like to : - extract the b =3D floor(k/2) bits leading the first 1 starting from MSB a= nd convert them to an integer : B - extract the remaining e =3D Ceiling (k/2) bits and convert them to an int= eger :=A0E. then I =3D 2^k + B * 2^b + E How can I do that efficiently in Ada ? I mean I thought of using boolean ar= rays 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.=20 Any suggestion ? Kind regards, Vincent