sbyte understanding needed

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

    sbyte understanding needed

    Hello i'm just starting out with csharp.

    and am trying to figure out why an sbyte can take any value from '-128'
    to 128.

    i've drawn a BIT box with eight bytes, and I'm assuming the eighth bit
    is used to SIGN the number, which leaves seven bits.

    when all 7 bits are on you get : 127
    so i was assuming the most you can get negative would be 127, and the
    eighth bit would be used to sign the fact that the number was
    negative...

    where does the extra one to make -128 come from?

    If i've totally misunderstood the handling of this variable in the
    computers memory.. please advise.

    thanks,

    Gary.

  • garyusenet@myway.com

    #2
    Re: sbyte understanding needed

    -128 to 127 i mean.. sorry.

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: sbyte understanding needed

      garyusenet@mywa y.com wrote:[color=blue]
      > Hello i'm just starting out with csharp.
      >
      > and am trying to figure out why an sbyte can take any value from '-128'
      > to 128.
      >
      > i've drawn a BIT box with eight bytes, and I'm assuming the eighth bit
      > is used to SIGN the number, which leaves seven bits.
      >
      > when all 7 bits are on you get : 127
      > so i was assuming the most you can get negative would be 127, and the
      > eighth bit would be used to sign the fact that the number was
      > negative...
      >
      > where does the extra one to make -128 come from?
      >
      > If i've totally misunderstood the handling of this variable in the
      > computers memory.. please advise.[/color]

      sbyte is stored as a two's complement number. See
      http://en.wikipedia.or g/wiki/Two's_complemen t
      for more information.

      Jon

      Comment

      • Nick Hounsome

        #4
        Re: sbyte understanding needed



        <garyusenet@myw ay.com> wrote in message
        news:1138191002 .801114.105040@ o13g2000cwo.goo glegroups.com.. .[color=blue]
        > Hello i'm just starting out with csharp.
        >
        > and am trying to figure out why an sbyte can take any value from '-128'
        > to 128.
        >
        > i've drawn a BIT box with eight bytes, and I'm assuming the eighth bit
        > is used to SIGN the number, which leaves seven bits.
        >
        > when all 7 bits are on you get : 127
        > so i was assuming the most you can get negative would be 127, and the
        > eighth bit would be used to sign the fact that the number was
        > negative...
        >
        > where does the extra one to make -128 come from?
        >
        > If i've totally misunderstood the handling of this variable in the
        > computers memory.. please advise.
        >
        > thanks,
        >
        > Gary.
        >[/color]


        Comment

        • Andy Walldorff

          #5
          Re: sbyte understanding needed

          A byte can have 256 distinct values. In this case (I assume) the high order
          bit is used as the sign. Therefore positive values are 0x0000 - 0x7fff and
          negative values are 0x8000 - 0xffff (or perhap more appropriately 0xffff
          (-1) to 0x8000 (-128)). The key here is that 0 is considered a postive
          value since the sign bit is not set.

          <garyusenet@myw ay.com> wrote in message
          news:1138191056 .397725.118230@ g44g2000cwa.goo glegroups.com.. .[color=blue]
          > -128 to 127 i mean.. sorry.
          >[/color]


          Comment

          • garyusenet@myway.com

            #6
            Re: sbyte understanding needed

            For anyone else seeking the answer to the question I asked - I think
            i've managed to find the answer by following the links the people in
            this group kindly contributed.

            Please - if anyone is reading this, can you confirm or refute what i'm
            about to say!

            The reason the highest negative number has a magnitude greater by 1,
            than the highest positive number is as follows.

            When all seven bits are off including the most significant bit (which
            is used to denote the sign of the number) the number is read as zero
            (i.e. the sign is positive, and the number is 0 )
            BUT.. when the most significant bit is on and all the other bits are
            off, this number is taken as the highest negative number available, as
            (-0 doesn't make sense) - so in this case its -128, which is why the
            maximum negative number has a magnitude greater than the highest
            positive number by one.

            =)

            Comment

            • Scott C

              #7
              Re: sbyte understanding needed

              garyusenet@mywa y.com wrote:[color=blue]
              > For anyone else seeking the answer to the question I asked - I think
              > i've managed to find the answer by following the links the people in
              > this group kindly contributed.
              >
              > Please - if anyone is reading this, can you confirm or refute what i'm
              > about to say!
              >
              > The reason the highest negative number has a magnitude greater by 1,
              > than the highest positive number is as follows.
              >
              > When all seven bits are off including the most significant bit (which
              > is used to denote the sign of the number) the number is read as zero
              > (i.e. the sign is positive, and the number is 0 )
              > BUT.. when the most significant bit is on and all the other bits are
              > off, this number is taken as the highest negative number available, as
              > (-0 doesn't make sense) - so in this case its -128, which is why the
              > maximum negative number has a magnitude greater than the highest
              > positive number by one.
              >
              > =)
              >[/color]

              I read the wiki article just like you and I started to think of it this way:

              positive numbers are like this:
              00000000 = 0
              00000001 = 1
              00000010 = 2 and so on. That is, they go "up" from zero, countingwise.

              negative numbers do the same thing (but with the signbit on), but
              instead of the number "origin" at zero, it's at -128:
              10000000 = -128
              10000001 = -127 etc...

              at least that's my take on it at any rate.

              Scott

              Comment

              Working...