473,239 Members | 1,570 Online

# 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

thanks,

Gary.

Jan 25 '06 #1
6 1812
-128 to 127 i mean.. sorry.

Jan 25 '06 #2
ga********@myway.com wrote:
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

sbyte is stored as a two's complement number. See
http://en.wikipedia.org/wiki/Two's_complement

Jon

Jan 25 '06 #3
http://en.wikipedia.org/wiki/2%27s_complement_notation

<ga********@myway.com> wrote in message
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

thanks,

Gary.

Jan 25 '06 #4
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.

<ga********@myway.com> wrote in message
-128 to 127 i mean.. sorry.

Jan 25 '06 #5
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

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.

=)

Jan 25 '06 #6
ga********@myway.com wrote:
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

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.

=)

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
Jan 25 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.