Signed/unsigned int c++?

What is an signed and unsigned integer number? I know that an unsigned integer number holds zero and positive numbers; signed, vis versa but including negative numbers. Why would you want to use these statements in a c++ program? how do they work in a c++ program -- what is their function/purpose? how do they help the program out in any way? Please explain and provide examples if possible.
Thanks!
closed account (zb0S216C)
psalm62 wrote:
"What is an signed and unsigned integer number? I know that an unsigned integer number holds zero and positive numbers; signed, vis versa but including negative numbers. Why would you want to use these statements in a c++ program?"

You've just answered your own question. In some cases, signed integers are more advantageous over unsigned integers due to the negative range, whereas unsigned integers are more advantageous due to the increased positive range -- It's a trade off. However, in some cases, it doesn't matter whether an integer is either signed or unsigned, so long as it [the integer] can hold a minimum and/or maximum range.

psalm62 wrote:
"how do they work in a c++ program"

Are you referring to how an integer is treated based on its signed-ness? If so, then you should know that the sign bit is a single bit of the entire integer storage that flags the integer storage as either signed or unsigned. When the sign bit is set, the computer knows that the integer has a negative range; otherwise, the computer knows the integer has only a positive range. Usually, the sign bit resides where the most significant bit is in unsigned integer storage.

psalm62 wrote:
"what is their function/purpose?"

To allow two different integral ranges.

Here's some additional information: "int", which is signed, is known as the most natural integer. When you specify a magic constant in C/++ without a suffix, such as "u" and "l", the magic constant is a signed integer.

Wazzak
Last edited on
Why would you want to use these statements in a c++ program?


Internally numbers are stored in a binary representation.

256 = 2*2*2*2*2*2*2*2 = 8 bits = 1 byte = 256 different combinations = 0 to 255(1 to 256) = 10000000 (binary representation)

1 bit is either 0 or 1, gives it 2 possible combinations.
However, if grouped together with other bits, the total possible combinations increases by a power of 2(^2), as their added (because each bit has two possible combinations).

Now an int of signed or unsigned, would in both cases consume same amount of bits.
But in order to have and int perform 'negative' values but still occupy the same amount of bits, you would have to sacrifice some of it's positive numbers. Because after all you still occupy, the same amount of bits/combinations.

To summarize: By using a 'signed type', you would be able to calculate on 'negative' values, but you'd lose (in the case of c++) 50% of it's 'positive' range.
This means that an 'unsigned type' is able to calculate on twice the amount of 'positive' numbers than a 'signed type'.

how do they help the program out in any way? Please explain and provide examples if possible.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*Say you have an array with 256(0 to 255) different elements.
 *Now instead of allocating an integer(which consumes, way more space than we need)
 *You could just declare a variable of unsigned char to cycle through the elements in a loop.
 *And in the process same that extra space(and processing power used to allocate it) for something else, making your code faster and more efficient.
 **/

int main(){

short array[256];

//by using an unsigned char, as opposed to an int you'd save yourself 
//3 bytes.
for(unsigned char x = 0;x<256;++x){
array[x] = 0;
}

return 0;
}



I hope this helps to some extent.
But i recommend to you that you google the following:
1. stack c++,
2. binary number base,
3. bytes,
4. c++ datatypes.

2,3, and 4 are especially useful to know about when you are dealing with very large numbers(numbers that begin reaching the max range of a datatype).

Last edited on
IndieExe, I tried you're code out in my compiler, and, even adding the
#include <iostream>
      #include "stdafx.h"
,
there's still bugs in the code.
Oh, that's the result of:
x<256
Should really be x<255

This is because, the max range for an unsigned char is 255, if it goes beyond, it's reset to zero.. Endless circle.

Do this instead:

1
2
3
4
while(x<=255){

if(x<255)++x;	
}
1
2
3
4
5
unsigned char uc = 0;
do
{
    //stuff with uc
}while(uc++ < std::numeric_limits<unsigned char>::max());
IndieExe, replace
short array[256];

//by using an unsigned char, as opposed to an int you'd save yourself 
//3 bytes.
for(unsigned char x = 0;x<256;++x){
array[x] = 0;
]

with
while(x<=255){

if(x<255)++x;	
}
?
Last edited on
I'm coming to the conclusion according to what my compiler is telling me that there should be another header included in the code for the unsigned char. Look here:
unsigned char.cpp(6): warning C4627: '#include <iostream>': skipped when looking for precompiled header use
psalms62 wrote:
how do they help the program out in any way?

As people have stated, using unsigned can increase your positive maximum two fold. If you're also doing lots of arithmetic that's time sensitive, unsigned could be faster. Though this isn't really an issue with CPU speeds anymore.
Also, sometimes you may need to be sure that a number can never be negative. Using an unsigned type can be a convenient approach - but it depends on the circumstances as to whether this is useful or not.
Last edited on
@psalm62 you need to create a new BLANK project, not a Win32 COnsole application - otherwise VC++ tries to use precompiled headers (it tries to screw you over).
Topic archived. No new replies allowed.