English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I understand how negative numbers are stored in memory by using Two-Compliment but i cant see any reason for the existance of a signed integer and unsigned integer in the language C (and its many derivatives) Surely once the number is signed there should be no difference in the handling of the data.

2006-10-14 10:48:26 · 5 answers · asked by Anonymous in Computers & Internet Programming & Design

I suddenly thought of the place wear a problem could exist and it is with buffers(like metioned) but it would require me to be asking for huge (2^31) portions of memory or doing something stupid (asking for -5 bytes of memory) im not gonna do either

2006-10-14 11:31:20 · update #1

5 answers

yes but you still have to know what it is when you pass its value around between functions.

Nevertheless I do agree with you. It's pretty rare that you would actually need to store a number as "unsigned" - the only obvious virtue is a very small space saving. So why go to the trouble?

Most of the time, signed integers will "cast" perfectly well to functions that expect unsigned ones, as long as they are actually not negative, and vice versa.

So stick to signed (the default). It avoids stupid errors. If an integer went negative that shouldn't, you might be hoping to trap that later in some check somewhere that handles it gracefully, but if it's an unsigned integer your code might die before you even get there.

Note, an index to an array makes perfect sense if it's negative (though instant code death is likely because you're probably in someone else's memory space - oops).

2006-10-14 10:54:22 · answer #1 · answered by wild_eep 6 · 0 0

It affects the range of a type. An unsigned byte can store anything in the range 0..255, but a signed byte can store anything in the range -128..127. Yes, technically there is no difference in the handling of the data, but let's face it, it'd be a real pain if you didn't have the choice. The simple fact is that they make life easier for the program. You have to remember that these conventions were devised by people with a lot more knowledge and experience than yourself, and for the most part you should just respect this wisdom and go along with it.

Rawlyn.

2006-10-14 11:54:54 · answer #2 · answered by Anonymous · 0 0

there's a huge distinction. A doggy needs housebreaking. you are able to't do this if you're away 8 hours an afternoon. A doggy ought to be socialized. you are able to't do this if you're actually not residing house. A doggy needs a number of of time and interest throughout the day to end it transforming into distructive. if you're at paintings all day the in difficulty-free words thanks to prevent the distruction is to go away it in a crate all day. by technique of the time it turns right into a dogs this is going to already be housebroken and function extra administration of this is actual applications so yet it would want to flow from the time you flow to paintings until eventually the time you get residing house. A dogs might want to be socialized by technique of the time it is a dogs and now no longer a doggy. A dogs frequently might want to particularly sleep all day and be waiting for play and interest at the same time as this is proprietor receives residing house. this is going to also be experienced so it isn't distructive once you're lengthy previous so it received't opt to be left in a crate for 8 hours an afternoon.

2016-12-04 20:12:49 · answer #3 · answered by friedman 4 · 0 0

Please listen to Steve Gibson's Security Now podcasts about buffer overflow exploits. Signed vs unsigned is huge.

2006-10-14 11:26:12 · answer #4 · answered by spookiestone 1 · 0 0

Some things aren't meant to make sense.

2006-10-14 10:49:40 · answer #5 · answered by Joker 3 · 0 0

fedest.com, questions and answers