From: Yutaka OIWA <oiwa@yl.is.s.u-tokyo.ac.jp>
To: Brian Hurt <brian.hurt@qlogic.com>
Cc: Ocaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Bug? Printf, %X and negative numbers
Date: 29 Mar 2003 07:21:14 +0900 [thread overview]
Message-ID: <vfiadff16at.fsf@tuba.is.s.u-tokyo.ac.jp> (raw)
In-Reply-To: Brian Hurt's message of "Fri, 28 Mar 2003 15:19:16 -0600 (CST)"
>> 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
next prev parent reply other threads:[~2003-03-28 22:21 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-03-28 21:19 Brian Hurt
2003-03-28 22:21 ` Yutaka OIWA [this message]
2003-03-30 9:51 ` Xavier Leroy
2003-03-31 15:44 ` Brian Hurt
2003-03-31 17:13 ` Ville-Pertti Keinonen
2003-04-01 8:19 ` Xavier Leroy
2003-04-01 16:09 ` David Brown
2003-04-01 16:45 ` Tim Freeman
2003-04-01 18:59 ` Brian Hurt
2003-04-01 19:16 ` Ville-Pertti Keinonen
2003-04-01 19:23 ` Tim Freeman
2003-04-01 21:00 ` Ville-Pertti Keinonen
2003-04-01 19:56 ` Brian Hurt
2003-04-01 20:45 ` Ville-Pertti Keinonen
2003-04-01 21:03 ` Brian Hurt
2003-04-02 8:55 ` Andreas Rossberg
2003-04-02 9:20 ` Ville-Pertti Keinonen
2003-04-01 18:34 ` Ville-Pertti Keinonen
2003-04-02 11:44 ` Claude Marche
2003-04-02 18:42 Gregory Morrisett
2003-04-02 21:12 ` Ville-Pertti Keinonen
2003-04-02 21:46 ` Lauri Alanko
2003-04-03 17:40 ` Ville-Pertti Keinonen
2003-04-04 16:14 ` Brian Hurt
2003-04-04 17:14 ` Ville-Pertti Keinonen
2003-04-04 17:27 ` Falk Hueffner
2003-04-03 0:52 ` brogoff
2003-04-03 9:29 Fabrice Le Fessant
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=vfiadff16at.fsf@tuba.is.s.u-tokyo.ac.jp \
--to=oiwa@yl.is.s.u-tokyo.ac.jp \
--cc=brian.hurt@qlogic.com \
--cc=caml-list@inria.fr \
/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