signed to unsigned. How does this work?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert Smith

    #1

    signed to unsigned. How does this work?

    Why/How does this work?

    I know that if I want to convert a signed byte (ie 0x7F) to unsigned
    number I can promote it to an integer, like this:

    byte a = (byte)0xAB; // -85
    int b = a & 0xFF; // 171

    The bit pattern is 10101011 (0xAB)
    If I AND it with 11111111 (0xFF)
    The result is 10101011

    ie. the same number! so why does this change the number to an unsigned
    (positive) number?


  • Lew

    #2
    Re: signed to unsigned. How does this work?

    Robert Smith wrote:
    Why/How does this work?
    >
    I know that if I want to convert a signed byte (ie 0x7F) to unsigned
    number I can promote it to an integer, like this:
    >
    byte a = (byte)0xAB; // -85
    int b = a & 0xFF; // 171
    >
    The bit pattern is 10101011 (0xAB)
    If I AND it with 11111111 (0xFF)
    The result is 10101011
    >
    ie. the same number!
    It is not the same number. You left out all the high bits where the
    difference is apparent.

    a is a byte, b is an int.

    a promotes to int in order to participate in the mask operation; the promoted
    value is
    0xFFFFFFAB.

    Because you masked out the high bits of a's promoted value on purpose, b is
    0x000000AB.

    Quite different.
    so why does this change the number to an unsigned (positive) number?
    Because you masked out all the high bits, including the sign bit, on purpose.

    0xFF is an int, and a positive one at that.

    0xFF == 0x000000FF

    The result of the & is 0x000000AB.

    --
    Lew

    Comment

    • Robert Smith

      #3
      Re: signed to unsigned. How does this work?

      Thanks so much for explaining that

      "Lew" <lew@nospam.lew scanon.comwrote in message
      news:LcGdnRlLK8 aWzMnbnZ2dnUVZ_ vWtnZ2d@comcast .com...
      Robert Smith wrote:
      Why/How does this work?

      I know that if I want to convert a signed byte (ie 0x7F) to unsigned
      number I can promote it to an integer, like this:

      byte a = (byte)0xAB; // -85
      int b = a & 0xFF; // 171

      The bit pattern is 10101011 (0xAB)
      If I AND it with 11111111 (0xFF)
      The result is 10101011

      ie. the same number!
      >
      It is not the same number. You left out all the high bits where the
      difference is apparent.
      >
      a is a byte, b is an int.
      >
      a promotes to int in order to participate in the mask operation; the
      promoted
      value is
      0xFFFFFFAB.
      >
      Because you masked out the high bits of a's promoted value on purpose, b
      is
      0x000000AB.
      >
      Quite different.
      >
      so why does this change the number to an unsigned (positive) number?
      >
      Because you masked out all the high bits, including the sign bit, on
      purpose.
      >
      0xFF is an int, and a positive one at that.
      >
      0xFF == 0x000000FF
      >
      The result of the & is 0x000000AB.
      >
      --
      Lew

      Comment

      • Lew

        #4
        Re: signed to unsigned. How does this work?

        Robert Smith wrote:
        Thanks so much for explaining that
        Your avoidance of top-posting in the future will be thanks aplenty.

        --
        Lew

        Comment

        Working...