471,315 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,315 software developers and data experts.

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.

Jan 25 '06 #1
6 1718
-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
computers memory.. please advise.


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

Jon

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

<ga********@myway.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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.

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
news:11**********************@g44g2000cwa.googlegr oups.com...
-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
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.

=)

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
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.

=)


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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Nick | last post: by
3 posts views Thread by Stephen Gennard | last post: by
3 posts views Thread by Steve - DND | last post: by
2 posts views Thread by Ray Ackley | last post: by
1 post views Thread by Darrel | last post: by
5 posts views Thread by Pat Ireland | last post: by
2 posts views Thread by K Viltersten | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.