How to ignore the signed bit?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chanma@163.com

    How to ignore the signed bit?

    I have an num -569360386, and turn it into hex format.
    I use toString(16),I get -21efc002.

    But how can get 0xDE103FFE,whic h is to ignore the highest bit as the
    signed bit?

  • Roedy Green

    #2
    Re: How to ignore the signed bit?

    On 22 Jan 2006 02:47:08 -0800, chanma@163.com wrote, quoted or
    indirectly quoted someone who said :
    [color=blue]
    >I have an num -569360386, and turn it into hex format.
    >I use toString(16),I get -21efc002.
    >
    >But how can get 0xDE103FFE,whic h is to ignore the highest bit as the
    >signed bit?[/color]
    see http://mindprod.com/jgloss/hex.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.

    Comment

    • Evertjan.

      #3
      Re: How to ignore the signed bit?

      Roedy Green wrote on 22 jan 2006 in comp.lang.javas cript:
      [color=blue]
      > On 22 Jan 2006 02:47:08 -0800, chanma@163.com wrote, quoted or
      > indirectly quoted someone who said :
      >[color=green]
      >>I have an num -569360386, and turn it into hex format.
      >>I use toString(16),I get -21efc002.
      >>
      >>But how can get 0xDE103FFE,whic h is to ignore the highest bit as the
      >>signed bit?[/color][/color]
      [color=blue]
      > see http://mindprod.com/jgloss/hex.html[/color]

      That is about Java, not Javascript!

      -21efc002 seems correct to me

      =============== ===========

      n = -569360386

      document.write( n.toString(16)+ '<br>'); // -21efc002.

      n = 569360386

      document.write( n.toString(16)) ; // 21efc002.

      --
      Evertjan.
      The Netherlands.
      (Please change the x'es to dots in my emailaddress)

      Comment

      • chanma@163.com

        #4
        Re: How to ignore the signed bit?

        Then is there any way to Get 0xDE103FFE but not -21efc002???

        Must I use my own code to get translate it?

        Comment

        • Evertjan.

          #5
          Re: How to ignore the signed bit?

          wrote on 22 jan 2006 in comp.lang.javas cript:
          [color=blue]
          > Then is there any way to Get 0xDE103FFE but not -21efc002???
          >[/color]

          Please quote what you are replying to.

          This is not email.

          If you want to post a followup via groups.google.c om, don't use the
          "Reply" link at the bottom of the article. Click on "show options" at the
          top of the article, then click on the "Reply" at the bottom of the article
          headers.
          [color=blue]
          > Must I use my own code to get translate it?[/color]

          0xDE103FFE is not an output in javascript

          The solution seems simple as each figure is the 16-complement:


          n = '-21efc002';

          n = n.toUpperCase() ;
          if (n.charAt(0)=='-'){
          r = 'FEDBCA98765432 10';
          m = '';
          for (i=1;i<n.length ;i++){
          g = n.charCodeAt(i)-48;
          if (g>9)g=g-7;
          m += r.charAt(g);
          };
          n = m;
          };
          n = '0x' + n

          document.write( n); // 0xDE103FFD

          --
          Evertjan.
          The Netherlands.
          (Please change the x'es to dots in my emailaddress)

          Comment

          • Roedy Green

            #6
            Re: How to ignore the signed bit?

            On Sun, 22 Jan 2006 11:04:14 GMT, Roedy Green
            <my_email_is_po sted_on_my_webs ite@munged.inva lid> wrote, quoted or
            indirectly quoted someone who said :
            [color=blue][color=green]
            >>But how can get 0xDE103FFE,whic h is to ignore the highest bit as the
            >>signed bit?[/color]
            > see http://mindprod.com/jgloss/hex.html[/color]

            sorry. That's the java answer to your question.
            --
            Canadian Mind Products, Roedy Green.
            http://mindprod.com Java custom programming, consulting and coaching.

            Comment

            • Evertjan.

              #7
              Re: How to ignore the signed bit?

              Evertjan. wrote on 22 jan 2006 in comp.lang.javas cript:
              [color=blue]
              > 0xDE103FFE is not an output in javascript
              >
              > The solution seems simple as each figure is the 16-complement:
              >
              >
              > n = '-21efc002';
              >
              > n = n.toUpperCase() ;
              > if (n.charAt(0)=='-'){
              > r = 'FEDBCA98765432 10';
              > m = '';
              > for (i=1;i<n.length ;i++){
              > g = n.charCodeAt(i)-48;
              > if (g>9)g=g-7;
              > m += r.charAt(g);
              > };
              > n = m;
              >};
              > n = '0x' + n
              >
              > document.write( n); // 0xDE103FFD
              >[/color]

              I leave the obvious difference of 1 to your coding ability.

              --
              Evertjan.
              The Netherlands.
              (Please change the x'es to dots in my emailaddress)

              Comment

              • chanma

                #8
                Re: How to ignore the signed bit?


                Evertjan. wrote:[color=blue]
                > Evertjan. wrote on 22 jan 2006 in comp.lang.javas cript:
                >[color=green]
                > > 0xDE103FFE is not an output in javascript
                > >[/color][/color]

                But in such code below:

                var x=0xf0000000;
                alert(x);

                it can output :4026531840 !

                So how the JS engine interprets it as unsigend numbers??

                Comment

                • Lasse Reichstein Nielsen

                  #9
                  Re: How to ignore the signed bit?

                  chanma@163.com writes:
                  [color=blue]
                  > I have an num -569360386, and turn it into hex format.
                  > I use toString(16),I get -21efc002.
                  >
                  > But how can get 0xDE103FFE,whic h is to ignore the highest bit as the
                  > signed bit?[/color]

                  Since numbers in Javascript are 64-bit IEEE floating point numbers,
                  the "highest bit" probably isn't what you think it is.
                  You can convert -123121512512451 25125 to hex as well.

                  What you might want it to treat the number as a 32 bit integer.
                  Luckily, that is an internal datatype in Javascript, and some
                  operations (in particular bitwise ones) does convert/truncate the
                  number to a 32 bit integer before operating on it. However, that
                  number can still be signed.

                  try:
                  function myToHex(num) {
                  num <<= 0; // truncate to 32 bits;
                  if (num < 0) { num = num ^ (1<<31); }
                  return "0x" + num.toString(16 );
                  }

                  /L
                  --
                  Lasse Reichstein Nielsen - lrn@hotpop.com
                  DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                  'Faith without judgement merely degrades the spirit divine.'

                  Comment

                  • Jonas Raoni

                    #10
                    Re: How to ignore the signed bit?

                    chanma@163.com escreveu:[color=blue]
                    > Then is there any way to Get 0xDE103FFE but not -21efc002???
                    > Must I use my own code to get translate it?[/color]

                    Since
                    0xDE103FFE = 3725606910 = 110111100001000 000111111111111 10
                    And
                    ~0xDE103FFE + 1 = 569360386 = 100001111011111 100000000000010

                    A xor operation (569360386 ^ 0xffffffff) + 1 would solve your problem:
                    111111111111111 111111111111111 11 [XOR]
                    001000011110111 111000000000000 10
                    --------------------------------------------------------
                    110111100001000 000111111111111 01 [+ 1] =
                    110111100001000 000111111111111 10

                    But due to the range of numbers available to binary operations, it
                    won't work :)

                    So you must make a work around like this one:

                    x = 569360386;
                    alert(((0x7ffff fff ^ x) + 0x80000001).toS tring(16));

                    I've tested it just with your example number (569360386), so test a
                    little for me haha =)


                    --
                    Jonas Raoni Soares Silva


                    Comment

                    • Dr John Stockton

                      #11
                      Re: How to ignore the signed bit?

                      JRS: In article <1137926828.619 440.110350@g14g 2000cwa.googleg roups.com>
                      , dated Sun, 22 Jan 2006 02:47:08 remote, seen in
                      news:comp.lang. javascript, chanma@163.com posted :[color=blue]
                      >I have an num -569360386, and turn it into hex format.
                      >I use toString(16),I get -21efc002.
                      >
                      >But how can get 0xDE103FFE,whic h is to ignore the highest bit as the
                      >signed bit?[/color]

                      "0x" + (Math.pow(2, 32) - 569360386).toSt ring(16).toUppe rCase()

                      To accommodate the widest possible range, you may need different code
                      for positive and negative, or to add leading zeroes, or to form

                      "0x" + (Math.pow(2, 52) - 569360386).toSt ring(16).toUppe rCase()

                      and select the required number of digits.

                      If done repeatedly, the power of 2 should be written as a constant :

                      "0x" + (0x100000000000 00 - 569360386).toSt ring(16).toUppe rCase()

                      --
                      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                      <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
                      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                      Comment

                      • alexey@shtokalo.net

                        #12
                        Re: How to ignore the signed bit?

                        I think following code mach easy :)

                        function toHexString(_in tValue)
                        {
                        var intHigh = (_intValue >> 16) & 0xFFFF;
                        return "0x" + intHigh.toStrin g(16) + (_intValue &
                        0xFFFF).toStrin g(16);
                        }

                        Comment

                        Working...