Part II of the small book that teaches you the C++ data types. Even if you have an IQ under 25 you will can understand and use the data types. In this part you will see an important aspect of data types... signed and unsigned.
Signed / Unsigned
Zero does make a difference
Letâ€™s suppose we have a data type that can hold 6 different numbers. That means that it can hold numbers 0, 1, 2, 3, 4 and 5. Many make the mistake and think that 6 numbers are the numbers from 1 to 6. Others make a bigger mistake, and say that it can hold numbers from 0 to 6. Computers use the number 0 as any other number – you have to remember that. 0 is the first positive number. 1 is the second. We have 6 spaces for 6 numbers, these numbers start from 0 and end with 5:
I hope you agree now, that if we say a data type holds x values, it actually holds the values from 0 to x-1. For example if a data type holds 1000 values, it actually holds the numbers from 0 to 999.
Positive and negative
Until now, we only saw fictive examples of data types that hold positive values. However, in computer programming negative numbers are used often. Are there any special data types that hold only negative numbers? No. Usual data types, that you will learn about in the following lessons, can be of two types, ones that hold only positive numbers, and ones that hold positive and negative numbers.
Letâ€™s take our previous example, the data type that holds 6 numbers, from 0 to 5. Letâ€™s suppose that this fictive data-type can hold negative numbers too. However, there are only 6 slots that can hold numbers, and all are positive, there is no more room for negative numbers. No problem, we take one slot and reserve it for the â€˜-â€˜ (minus) sign. This helps because negative numbers look the same as positive numbers, just that they have the minus sign in front of them. Therefore, in our example, with the numbers 0, 1, 2, 3, 4 and 5 we have:
You can observe that 0 is considered here a positive number, just as I said earlier.
Signed and unsigned
You probably realized that you have just learned what signed and unsigned data types are.
Letâ€™s review the previous examples:
This is an unsigned data type, because it accepts only positive values.
Now look at this example:
This is a signed data type, it splits in two types of numbers, positive and negative.
When you use an unsigned data type, you must specify it, and youâ€™ll see later in this book how this can be done. When you use an signed data type, you arenâ€™t supposed to specify it, because data types that donâ€™t have this specified, are signed by default (they support positive and negative numbers too).
Advantages and disadvantages
Probably you already understand what advantages / disadvantages the use dof unsigned / signed data types involve.
Using unsigned data types itâ€™s better for some instances. For example if you have a program in which there are calculated the number of guests, tables, chairs, plates, etc., for a party. You can never have a negative number of guests, therefore you should use the unsigned data type, for gaining more numbers in positive field. You donâ€™t need the useless negative numbers.
On the other side, if you make a program that holds your bank account information, with your daily income, spending and debt, you should use signed version of that data type. Because if you are in debt, the money left in your account will be represented with minus (ex.: -12530$).
For the computer it makes no difference if you use signed or unsigned version of the data type, regarding the memory space or performance.
You can continue with ‘Understanding C++ data types III’