From: Yaron Minsky <yminsky@gmail.com>
To: Jean-Christophe Filliatre <jean-christophe.filliatre@lri.fr>,
Caml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Counting bits in a big_int
Date: Fri, 14 May 2004 22:53:34 -0400 [thread overview]
Message-ID: <891bd3390405141953628db08a@mail.gmail.com> (raw)
In-Reply-To: <16547.27869.60461.270873@gargle.gargle.HOWL>
Here's the fastest bitcounter I've been able to come up with. It's
about an order of magnitude faster than just counting bit by bit.
let ( *! ) = mult_big_int
let ( +! ) = add_big_int
let ( -! ) = sub_big_int
let ( %! ) = mod_big_int
let ( /! ) = div_big_int
let ( **! ) = power_big_int_positive_int
let ( <>! ) x y = not (eq_big_int x y)
let ( =! ) = eq_big_int
let ( <! ) = lt_big_int
let ( >! ) = gt_big_int
let ( <=! ) = le_big_int
let ( >=! ) = ge_big_int
let nbits_slow x =
let rec loop i two_to_i =
if two_to_i >! x then i
else loop (succ i) (two *! two_to_i)
in
if x =! zero then 1 else loop 1 two
let nbits x =
let nwords = num_digits_big_int x in
let wsize = Sys.word_size in
let lowbits = (nwords - 1) * wsize in
let lastword = x /! two **! lowbits in
nbits_slow lastword + (nwords - 1) * wsize
On Thu, 13 May 2004 14:41:01 +0200, Jean-Christophe Filliatre
<jean-christophe.filliatre@lri.fr> wrote:
>
>
> Yaron Minsky writes:
> > What I'm actually interested in is finding the index of the rightmost
> > set bit. So, for the int:
> > 1000110, I'd want the answer to be 7, not 3.
>
> Ok; then Michael's suggestion should work, as follows:
>
> ======================================================================
> let count_bits b =
> let rec loop i two_to_i = (* inv 2^(i-1) <= b *)
> if gt_big_int two_to_i b then i
> else loop (succ i) (mult_int_big_int 2 two_to_i)
> in
> if eq_big_int b zero_big_int then 1 else loop 1 (big_int_of_int 2)
> ======================================================================
>
> --
> Jean-Christophe
>
>
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
next prev parent reply other threads:[~2004-05-15 2:53 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-05-12 3:22 Yaron Minsky
2004-05-12 4:19 ` Michael Hoisie
[not found] ` <16547.8441.559944.112854@gargle.gargle.HOWL>
[not found] ` <891bd3390405130427180c36d6@mail.gmail.com>
[not found] ` <16547.27869.60461.270873@gargle.gargle.HOWL>
2004-05-15 2:53 ` Yaron Minsky [this message]
2004-05-15 11:14 ` Markus Mottl
2004-05-15 20:19 ` Yaron Minsky
2004-05-16 0:51 ` skaller
2004-05-16 7:30 ` Xavier Leroy
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=891bd3390405141953628db08a@mail.gmail.com \
--to=yminsky@gmail.com \
--cc=caml-list@inria.fr \
--cc=jean-christophe.filliatre@lri.fr \
--cc=yminsky@cs.cornell.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).