From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p4RHFHnd032701 for ; Fri, 27 May 2011 19:15:17 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgIDAL/b303UGyoCkWdsb2JhbABUmC2ODhQBAQEBCQsLBxQDIohwvUKGHgSQQ4Q7HYo/ X-IronPort-AV: E=Sophos;i="4.65,281,1304287200"; d="scan'208";a="95588616" Received: from smtp2-g21.free.fr ([212.27.42.2]) by mail2-smtp-roc.national.inria.fr with ESMTP; 27 May 2011 19:15:11 +0200 Received: from [192.168.1.3] (unknown [82.237.71.191]) by smtp2-g21.free.fr (Postfix) with ESMTP id C16C54B0225 for ; Fri, 27 May 2011 19:15:04 +0200 (CEST) Message-ID: <4DDFDC1F.5050605@inria.fr> Date: Fri, 27 May 2011 19:15:11 +0200 From: Xavier Leroy User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.17) Gecko/20110424 Thunderbird/3.1.10 MIME-Version: 1.0 To: caml-list@inria.fr References: <707567.2845.qm@web111505.mail.gq1.yahoo.com> <833816.58675.qm@web111502.mail.gq1.yahoo.com> In-Reply-To: X-Enigmail-Version: 1.1.2 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Subject: Re: [Caml-list] Re: Binary logarithm of a power of 2 On 05/27/2011 03:26 PM, Sylvain Le Gall wrote: > Friday afternoon exercice ;-) Same here :-) > Here is my code: [ Recursive & array-based implementation in Caml ] Simpler and maybe faster: let logbin (n: int32) = let n = ref (Int32.add n 0l) and i = ref 0 in if !n >= 0x10000l then (n := Int32.shift_right !n 16; i := 16); if !n >= 0x100l then (n := Int32.shift_right !n 8; i := !i + 8); if !n >= 0x10l then (n := Int32.shift_right !n 4; i := !i + 4); if !n >= 0x4l then (n := Int32.shift_right !n 2; i := !i + 2); if !n >= 0x2l then !i + 1 else !i Have fun, - Xavier