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. 6 1819
-128 to 127 i mean.. sorry. 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 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.
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.
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.
=) 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |