From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id XAA22660; Fri, 28 Mar 2003 23:21:23 +0100 (MET) Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id XAA23397 for ; Fri, 28 Mar 2003 23:21:21 +0100 (MET) Received: from venus.is.s.u-tokyo.ac.jp (venus.is.s.u-tokyo.ac.jp [133.11.12.9]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id h2SMLJX00628 for ; Fri, 28 Mar 2003 23:21:20 +0100 (MET) Received: from tuba.is.s.u-tokyo.ac.jp (tuba.is.s.u-tokyo.ac.jp [133.11.12.102]) by venus.is.s.u-tokyo.ac.jp (8.11.6/3.7W) with ESMTP id h2SMLFG20678; Sat, 29 Mar 2003 07:21:15 +0900 (JST) Received: (from oiwa@localhost) by tuba.is.s.u-tokyo.ac.jp (8.11.6+Sun/3.7W) id h2SMLEP26166; Sat, 29 Mar 2003 07:21:14 +0900 (JST) X-Authentication-Warning: tuba.is.s.u-tokyo.ac.jp: oiwa set sender to oiwa@yl.is.s.u-tokyo.ac.jp using -f To: Brian Hurt Cc: Ocaml Mailing List Subject: Re: [Caml-list] Bug? Printf, %X and negative numbers References: From: Yutaka OIWA Date: 29 Mar 2003 07:21:14 +0900 In-Reply-To: Brian Hurt's message of "Fri, 28 Mar 2003 15:19:16 -0600 (CST)" Message-ID: User-Agent: Gnus/5.0807 (Gnus v5.8.7) Emacs/20.7 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Spam: no; 0.00; yutaka:01 oiwa:01 u-tokyo:01 caml-list:01 bug:01 printf:01 qlogic:01 3.06:01 byterun:01 val:01 sprintf:01 shibuya:01 ints:01 ocaml:01 caml:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk >> On Fri, 28 Mar 2003 15:19:16 -0600 (CST), Brian Hurt said: Brian> $ ocaml Brian> Objective Caml version 3.06 Brian> # Printf.printf "%08X\n" (1 lsl 30);; Brian> C0000000 Brian> - : unit = () Brian> # Printf.printf "%010X\n" (1 lsl 30);; Brian> 00C0000000 Brian> - : unit = () Brian> # Brian> I expected output of 40000000, not C0000000. Note that this isn't a case Brian> of simple sign extension, as the second example demonstrates. Where'd the Brian> extra bit come from? Brian> Brian The extra bit comes from sign extension: (1 lsl 30) is the minimal negative Caml integer. The cause exists in byterun/ints.c: 31bit-value 0x40000000 is sign-extended to 32bit C-integer 0xc0000000 by macro Long_val, then sprintf in the C library puts extra "00" in front of it (equivalent to zero-extension). Maybe the unsigned version of Long_val is needed. # 1 lsl 30 - 1;; - : int = 1073741823 # Printf.sprintf "%u" (1 lsl 30);; - : string = "3221225472" -- Yutaka Oiwa Yonezawa Lab., Dept. of Computer Science, Graduate School of Information Sci. & Tech., Univ. of Tokyo. , PGP fingerprint = C9 8D 5C B8 86 ED D8 07 EA 59 34 D8 F4 65 53 61 ------------------- 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