473,239 Members | 1,570 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,239 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 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Nick | last post by:
Hi, Problem statement: Calling a java method from Dot Net code that returns a java byte array. Overview: I have a java server from which I need to establish connection. I hva ebeen able to...
3
by: Stephen Gennard | last post by:
Hello, I having a problem dynamically invoking a static method that takes a reference to a SByte*. If I do it directly it works just fine. Anyone any ideas why? I have include a example...
3
by: Steve - DND | last post by:
After downloading and using two different memory profiling applications today, it seems that sbyte, int16, and int32 are all stored using 12b of memory. Someone please tell me I'm reading these...
2
by: Ray Ackley | last post by:
I need to convert a byte to an sbyte - and it has to preserve the original binary meaning of the 8 bits. For example - a 1111 1111, which is interpreted as byte to be 255, should show up in the...
1
by: Darrel | last post by:
I am using binary writer to write an array of bytes to disk. However, my data starts out as an array of sbytes. I am currently type casting each array element in a for loop. Is there a faster...
5
by: Pat Ireland | last post by:
I am attempting to pass an SByte to another .NET class that expects the data to be passed as SByte *. I searched around and found the boxing can be used to produce a pointer but I'm not sure if...
3
by: reju | last post by:
I have to convert an sByte array to byte array. This sbyte array contains some signed values due to this .net converting functions will throw an exception.any work around for this? is there any...
0
by: Ben Voigt [C++ MVP] | last post by:
Chip Gore wrote: Actually, because you are using the C++/CLI compiler, microsoft.public.dotnet.languages.vc is the most appropriate place to discuss this. To be useful from C# and other .NET...
2
by: K Viltersten | last post by:
I try the following. sbyte HalvOfFour () { sbyte b = 4; return b / 2; } Apparently, since 2 is regarded as int, the operation upgrades my b to and int and after it has been performed, i...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.