output confusion
-
Hi, I am bit confusing about output of fallowing variable 'a' which produce '-1' as result. sbyte a =unchecked((sbyte)255); I have not cleared yet why this store -1 plz clarify me.
no knowledge in .net
-
Hi, I am bit confusing about output of fallowing variable 'a' which produce '-1' as result. sbyte a =unchecked((sbyte)255); I have not cleared yet why this store -1 plz clarify me.
no knowledge in .net
-
Hi, I am bit confusing about output of fallowing variable 'a' which produce '-1' as result. sbyte a =unchecked((sbyte)255); I have not cleared yet why this store -1 plz clarify me.
no knowledge in .net
The sign is represented by the MSb (most significant bit).
Binary Hex byte sbyte
00000000 0x00 0 0
...
01111111 0x7F 127 127
10000000 0x80 128 -128
...
11111111 0xFF 255 - 1Dave
If this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)modified on Saturday, June 5, 2010 9:09 AM
-
Hi, I am bit confusing about output of fallowing variable 'a' which produce '-1' as result. sbyte a =unchecked((sbyte)255); I have not cleared yet why this store -1 plz clarify me.
no knowledge in .net
Because a
sbyte
is a "short byte", meaning it's max value is half of what a normal byte would hold. In your case, your value is causing an interger overflow condition, and the value becomes -1..45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001 -
Because a
sbyte
is a "short byte", meaning it's max value is half of what a normal byte would hold. In your case, your value is causing an interger overflow condition, and the value becomes -1..45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001I thought the 's' stood for signed rather than short as it's still 8 bits so no shorter. Either way the answer to the OP is the same and correct :thumbsup:
Dave
If this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
I thought the 's' stood for signed rather than short as it's still 8 bits so no shorter. Either way the answer to the OP is the same and correct :thumbsup:
Dave
If this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)You're right, however if we are going to be picky, maybe you'd better fix the mistake in
10000000 0x80 128 -127
too? :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
You're right, however if we are going to be picky, maybe you'd better fix the mistake in
10000000 0x80 128 -127
too? :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
;P :-O :laugh: Fixed!
Dave
If this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
You're right, however if we are going to be picky, maybe you'd better fix the mistake in
10000000 0x80 128 -127
too? :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
;P :-O :laugh: Fixed!
Dave
If this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Long ago Data General used to build 36-bit machines, where an integer by default was 36 bits; they also had instructions that treated registers holding five 7-bit "bytes" plus a single flag. They used that for representing text, based on ASCII characters, which requires 7 bits. That could rightfully be called short bytes. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
;P :-O :laugh: Fixed!
Dave
If this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)the 100...00 bit pattern happens to be the only one you can't represent the absolute value of; and lots of run-time libraries don't treat it well, e.g. an sprintf-like function turning a signed integer to its decimal string representation wants to do:
if (value) {
emit('-');
value = -value; // <<<<<<<<<
}
// now process positive numberwhich isn't correct, as value may still be negative yielding unexpected quotient/remainder values later on. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
Long ago Data General used to build 36-bit machines, where an integer by default was 36 bits; they also had instructions that treated registers holding five 7-bit "bytes" plus a single flag. They used that for representing text, based on ASCII characters, which requires 7 bits. That could rightfully be called short bytes. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
How to handle bit lengths for types values that don't require all of them or require more is still an issue in some situations. In my MIDI exploits I'm working with 16bit PICs at the moment. The choice between the complexity and therefore instruction cycle overhead of storing 2 bytes in one word (made more complex as packets can be variable lengths so won't always be word aligned) and the waste of scarce resources by ignoring 8 bits for every byte I need to buffer is one I'm still debating. MS[^] use one 32bit int (all packets, except SysEx which is easy to identify, are 1 to 3 bytes) and 'waste' between 8 and 24 bits each time. Fine on a PC but not efficient useage of RAM on a µC with only 2KB RAM when requiring 4 individual buffers.
Dave
If this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
How to handle bit lengths for types values that don't require all of them or require more is still an issue in some situations. In my MIDI exploits I'm working with 16bit PICs at the moment. The choice between the complexity and therefore instruction cycle overhead of storing 2 bytes in one word (made more complex as packets can be variable lengths so won't always be word aligned) and the waste of scarce resources by ignoring 8 bits for every byte I need to buffer is one I'm still debating. MS[^] use one 32bit int (all packets, except SysEx which is easy to identify, are 1 to 3 bytes) and 'waste' between 8 and 24 bits each time. Fine on a PC but not efficient useage of RAM on a µC with only 2KB RAM when requiring 4 individual buffers.
Dave
If this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)I tend to first take care of whatever resource is most scarce. On miniature systems that would be memory most of the time. So decide on a clever data scheme, then come up with efficient accessor functions, even if that takes some ugly code and hacks. Maybe create a stream-like object? :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).