Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Bug? Printf, %X and negative numbers
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Yutaka OIWA <oiwa@y...>
Subject: Re: [Caml-list] Bug? Printf, %X and negative numbers
>> On Fri, 28 Mar 2003 15:19:16 -0600 (CST), Brian Hurt <brian.hurt@qlogic.com> 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.
      <oiwa@yl.is.s.u-tokyo.ac.jp>, <yutaka@oiwa.shibuya.tokyo.jp>
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