Loading ...
Sorry, an error occurred while loading the content.

printf "%x" (-1)

Expand Messages
  • Radu Grigore
    Why does Printf.printf %x (-1);; displays 7f ff ff ff, instead of ff ff ff ff? regards, radu __________________________________ Do you Yahoo!? Win a $20,000
    Message 1 of 15 , Apr 29, 2004
    • 0 Attachment
      Why does

      Printf.printf "%x" (-1);;

      displays 7f ff ff ff, instead of ff ff ff ff?

      regards,
      radu





      __________________________________
      Do you Yahoo!?
      Win a $20,000 Career Makeover at Yahoo! HotJobs
      http://hotjobs.sweepstakes.yahoo.com/careermakeover
    • Michael
      ... OCAML-ints are 31 bit not 32 :-)
      Message 2 of 15 , Apr 29, 2004
      • 0 Attachment
        On Donnerstag, 29. April 2004 10:05, Radu Grigore wrote:
        > Why does
        >
        > Printf.printf "%x" (-1);;
        >
        > displays 7f ff ff ff, instead of ff ff ff ff?
        >

        OCAML-ints are 31 bit not 32 :-)
      • harigato2002
        ... Hi, I run the following test: Objective Caml version 3.06 # Printf.printf %x (-1);; ffffffff- : unit = () # I get ffffffff, and not 7f ff ff ff. Which
        Message 3 of 15 , Apr 29, 2004
        • 0 Attachment
          --- In ocaml_beginners@yahoogroups.com, Michael <micha-1@f...> wrote:
          > On Donnerstag, 29. April 2004 10:05, Radu Grigore wrote:
          > > Why does
          > >
          > > Printf.printf "%x" (-1);;
          > >
          > > displays 7f ff ff ff, instead of ff ff ff ff?
          > >
          >
          > OCAML-ints are 31 bit not 32 :-)

          Hi,

          I run the following test:

          Objective Caml version 3.06

          # Printf.printf "%x" (-1);;
          ffffffff- : unit = ()
          #

          I get ffffffff, and not 7f ff ff ff.
          Which version do you use?

          bye
          H
        • Nicolas Cannasse
          ... This problem have been corrected in 3.07. The good answer is 0x7FFFFFFF on 32 bit platforms. Nicolas Cannasse
          Message 4 of 15 , Apr 29, 2004
          • 0 Attachment
            > Hi,
            >
            > I run the following test:
            >
            > Objective Caml version 3.06
            >
            > # Printf.printf "%x" (-1);;
            > ffffffff- : unit = ()
            > #
            >
            > I get ffffffff, and not 7f ff ff ff.
            > Which version do you use?

            This problem have been corrected in 3.07.
            The good answer is 0x7FFFFFFF on 32 bit platforms.

            Nicolas Cannasse
          • Radu Grigore
            Here is copy & paste from my console: Objective Caml version 3.07+2 # Printf.printf %x (-1);; 7fffffff- : unit = () ... __________________________________ Do
            Message 5 of 15 , Apr 29, 2004
            • 0 Attachment
              Here is copy & paste from my console:

              Objective Caml version 3.07+2

              # Printf.printf "%x" (-1);;
              7fffffff- : unit = ()


              --- harigato2002 <harigato2002@...> wrote:
              > [...]
              > Hi,
              >
              > I run the following test:
              >
              > Objective Caml version 3.06
              >
              > # Printf.printf "%x" (-1);;
              > ffffffff- : unit = ()
              > #
              >
              > I get ffffffff, and not 7f ff ff ff.
              > Which version do you use?
              >
              > bye
              > H





              __________________________________
              Do you Yahoo!?
              Win a $20,000 Career Makeover at Yahoo! HotJobs
              http://hotjobs.sweepstakes.yahoo.com/careermakeover
            • Radu Grigore
              ... I ve found out (using google, can t remember the link right now) that this is because OCaml uses a bit to distinguish pointers from integers. Bleah! Why?
              Message 6 of 15 , Apr 30, 2004
              • 0 Attachment
                > > Printf.printf "%x" (-1);;
                > > displays 7f ff ff ff, instead of ff ff ff ff?
                >
                > OCAML-ints are 31 bit not 32 :-)

                I've found out (using google, can't remember the link right now) that
                this is because OCaml uses a bit to distinguish pointers from
                integers. Bleah! Why?

                Anyway, from the bug in 3.06 i _guess_ that integers have bit 31
                equal to 1, and bit 30 is the sign bit. Pointers are just native
                integers but assumed never to be negative. (are there checks for
                this?).

                regards,
                radu
                http://www.geocities.com/_rgrig





                __________________________________
                Do you Yahoo!?
                Win a $20,000 Career Makeover at Yahoo! HotJobs
                http://hotjobs.sweepstakes.yahoo.com/careermakeover
              • Remi Vanicat
                ... Well, I don t know what you mean by bit 31, but an ocaml int n is represented by the C long 2*n+1. By the way, there is not really a sign bit in standard
                Message 7 of 15 , Apr 30, 2004
                • 0 Attachment
                  Radu Grigore <radugrigore@...> writes:

                  >> > Printf.printf "%x" (-1);;
                  >> > displays 7f ff ff ff, instead of ff ff ff ff?
                  >>
                  >> OCAML-ints are 31 bit not 32 :-)
                  >
                  > I've found out (using google, can't remember the link right now) that
                  > this is because OCaml uses a bit to distinguish pointers from
                  > integers. Bleah! Why?
                  >
                  > Anyway, from the bug in 3.06 i _guess_ that integers have bit 31
                  > equal to 1, and bit 30 is the sign bit.

                  Well, I don't know what you mean by bit 31, but an ocaml int n is
                  represented by the C long 2*n+1. By the way, there is not really a
                  "sign bit" in standard representation of integer...

                  > Pointers are just native integers but assumed never to be
                  > negative. (are there checks for this?).

                  Nop, pointer are just aligned pointer, so always an even (unsigned)
                  integer. There is nothing to say about them to be positive or
                  negative (what does this mean for a pointer ???).

                  [...]


                  --
                  Rémi Vanicat
                • Frederic van der Plancke
                  ... To allow for garbage collection. The garbage collector has to be able to make the distinction some way, because it must follow pointers to know which areas
                  Message 8 of 15 , Apr 30, 2004
                  • 0 Attachment
                    Radu Grigore wrote:
                    >
                    > > > Printf.printf "%x" (-1);;
                    > > > displays 7f ff ff ff, instead of ff ff ff ff?
                    > >
                    > > OCAML-ints are 31 bit not 32 :-)
                    >
                    > I've found out (using google, can't remember the link right now) that
                    > this is because OCaml uses a bit to distinguish pointers from
                    > integers. Bleah! Why?

                    To allow for garbage collection. The garbage collector has to be able
                    to make the distinction some way, because it must follow pointers to
                    know which areas of memory are still in use. So-called "conservative"
                    garbage collector do *guess*, and make the conservative choice
                    when there is a doubt. But OCaml's collector does not work that way.
                    (I suppose better properties come from not having to guess.)

                    >
                    > Anyway, from the bug in 3.06

                    *I* am not convinced ffffffff is less good than 7fffffff. It's a matter
                    of convention. Signed 31-bit 7fffffff represents, after all, the very same
                    integer as signed 32-bit ffffffff. (If I could change the world, I would
                    enforce the first digit of the hexadecimal representation of an integer
                    to determine its sign regardless of context. Thus '7' would imply a
                    positive number. And '0x7fffffff' could be rejected or warned as out
                    of range for a 31-bit signed int.)

                    > i _guess_ that integers have bit 31
                    > equal to 1, and bit 30 is the sign bit. Pointers are just native
                    > integers but assumed never to be negative. (are there checks for
                    > this?).

                    Bit 0 is the bit used to distinguish between integers and pointers.
                    Bit 31 is the sign bit, but it moves to bit 30 when the OCaml integer
                    is transformed into an ordinary 32 bit integer before it's converted
                    to a string.

                    Frédéric
                  • Martin Jambon
                    ... The online O Reilly book has a chapter about garbage collection: http://caml.inria.fr/oreilly-book/html/index.html Martin
                    Message 9 of 15 , Apr 30, 2004
                    • 0 Attachment
                      On Fri, 30 Apr 2004, Radu Grigore wrote:

                      > > > Printf.printf "%x" (-1);;
                      > > > displays 7f ff ff ff, instead of ff ff ff ff?
                      > >
                      > > OCAML-ints are 31 bit not 32 :-)
                      >
                      > I've found out (using google, can't remember the link right now) that
                      > this is because OCaml uses a bit to distinguish pointers from
                      > integers. Bleah! Why?

                      The online O'Reilly book has a chapter about garbage collection:

                      http://caml.inria.fr/oreilly-book/html/index.html


                      Martin
                    • Hendrik Tews
                      ... %x converts to _unsigned_ hex. So 7fffffff is the right answer. What you want is signed hex (which does not exist), which probably should print -1 instead
                      Message 10 of 15 , Apr 30, 2004
                      • 0 Attachment
                        Frederic van der Plancke <fvdp@...> writes:

                        > Radu Grigore wrote:
                        > >
                        > > > > Printf.printf "%x" (-1);;

                        > *I* am not convinced ffffffff is less good than 7fffffff. It's a matter
                        > of convention. Signed 31-bit 7fffffff represents, after all, the very same
                        > integer as signed 32-bit ffffffff. (If I could change the world, I would

                        %x converts to _unsigned_ hex. So 7fffffff is the right answer.
                        What you want is signed hex (which does not exist), which
                        probably should print -1 instead of ffffffff.

                        Bye,

                        Hendrik
                      • Radu Grigore
                        ... The bit corresponding to 2^31. ... So how does it add/multiply/etc two integers? If bit 31 would have been used then (a+b)|0x80000000 and (a*b)|0x80000000
                        Message 11 of 15 , Apr 30, 2004
                        • 0 Attachment
                          --- Remi Vanicat <remi.vanicat@...> wrote:

                          > Well, I don't know what you mean by bit 31,

                          The bit corresponding to 2^31.

                          > but an ocaml int n is represented by the C long 2*n+1.

                          So how does it add/multiply/etc two integers? If bit 31 would have
                          been used then (a+b)|0x80000000 and (a*b)|0x80000000 would have been
                          enough. With 2n+1 representation usual operations seem to be more
                          troublesome. This is one other reason why I guessed bit 31 is used.

                          > There is nothing to say about them to be positive or
                          > negative (what does this mean for a pointer ???).

                          I meant they are negative when intepreted as signed integers: ie. I
                          wanted to say "bit 31 is zero" in yet another way (in case "bit 31"
                          is ambiguous).

                          regards,
                          radu





                          __________________________________
                          Do you Yahoo!?
                          Win a $20,000 Career Makeover at Yahoo! HotJobs
                          http://hotjobs.sweepstakes.yahoo.com/careermakeover
                        • Radu Grigore
                          ... This, together with Remi Vanicat s statement about pointer alignment made the reasons clear to me. But: 1) why are pointers always aligned? does the
                          Message 12 of 15 , Apr 30, 2004
                          • 0 Attachment
                            --- Frederic van der Plancke <fvdp@...> wrote:

                            > To allow for garbage collection.

                            This, together with Remi Vanicat's statement about pointer alignment
                            made the reasons clear to me.

                            But:
                            1) why are pointers always aligned? does the compiler ensure this?
                            AFAIK on Intel you can have unaligned pointers: it just works slower.

                            2) how are arithmetic operations done efficiently?

                            Now I'm going to the OReilly book.. may be it has an answer.

                            regards,
                            radu





                            __________________________________
                            Do you Yahoo!?
                            Win a $20,000 Career Makeover at Yahoo! HotJobs
                            http://hotjobs.sweepstakes.yahoo.com/careermakeover
                          • Nicolas Cannasse
                            ... Same answer as Remi : the GC ensure it :) ... I think the native code compiler is making some local optimizations based on type infered in order to avoid
                            Message 13 of 15 , Apr 30, 2004
                            • 0 Attachment
                              > But:
                              > 1) why are pointers always aligned? does the compiler ensure this?
                              > AFAIK on Intel you can have unaligned pointers: it just works slower.

                              Same answer as Remi : the GC ensure it :)

                              > 2) how are arithmetic operations done efficiently?

                              I think the native code compiler is making some local optimizations based on
                              type infered in order to avoid additionnal operations.
                              In general : adding a and b is done by adding the two minus one, or
                              doing : a + (b & ~1).

                              Nicolas Cannasse
                            • Remi Vanicat
                              ... if r(a)=2*a+1 and r(b)=2*b+1 then r(a+b)=r(a)+r(b)-1 and r(a*b)=(r(a)-1)*(r(b)/2)+1 (well, not sure for multiplication, but it is something like that, the
                              Message 14 of 15 , Apr 30, 2004
                              • 0 Attachment
                                Radu Grigore <radugrigore@...> writes:

                                > --- Remi Vanicat <remi.vanicat@...> wrote:
                                >
                                >> Well, I don't know what you mean by bit 31,
                                >
                                > The bit corresponding to 2^31.
                                >
                                >> but an ocaml int n is represented by the C long 2*n+1.
                                >
                                > So how does it add/multiply/etc two integers? If bit 31 would have
                                > been used then (a+b)|0x80000000 and (a*b)|0x80000000 would have been
                                > enough. With 2n+1 representation usual operations seem to be more
                                > troublesome. This is one other reason why I guessed bit 31 is used.

                                if r(a)=2*a+1
                                and r(b)=2*b+1
                                then r(a+b)=r(a)+r(b)-1
                                and r(a*b)=(r(a)-1)*(r(b)/2)+1
                                (well, not sure for multiplication, but it is something like that, the
                                r(a)-1 can be implemented using a xor, the /2 using shift, and the + 1
                                using another xor (xor are quicker than standard
                                addition/substraction).


                                ocamlopt probably can do some optimization for longer expression.

                                >
                                >> There is nothing to say about them to be positive or
                                >> negative (what does this mean for a pointer ???).
                                >
                                > I meant they are negative when intepreted as signed integers: ie. I
                                > wanted to say "bit 31 is zero" in yet another way (in case "bit 31"
                                > is ambiguous).

                                Well, then no, it just even. By the way is is simpler for pointer :
                                they are just not modified.

                                --
                                Rémi Vanicat
                              • Richard Jones
                                ... Actually not so. Ocamlopt does very smart things to optimize these operations: see: http://www.merjis.com/developers/ocaml_tutorial/ch11/ Rich. -- Richard
                                Message 15 of 15 , May 1, 2004
                                • 0 Attachment
                                  On Fri, Apr 30, 2004 at 04:21:06AM -0700, Radu Grigore wrote:
                                  > With 2n+1 representation usual operations seem to be more
                                  > troublesome.

                                  Actually not so. Ocamlopt does very smart things to optimize these
                                  operations: see: http://www.merjis.com/developers/ocaml_tutorial/ch11/

                                  Rich.

                                  --
                                  Richard Jones. http://www.annexia.org/ http://www.j-london.com/
                                  Merjis Ltd. http://www.merjis.com/ - improving website return on investment
                                  "One serious obstacle to the adoption of good programming languages is
                                  the notion that everything has to be sacrificed for speed. In computer
                                  languages as in life, speed kills." -- Mike Vanier
                                Your message has been successfully submitted and would be delivered to recipients shortly.