Integers

 Signed and Unsigned Numbers

An integer is a natural number typically used to count items. An integer is considered a "whole number" because it doesn't display a decimal fraction.

A number can be expressed as being positive or negative. When a number is written like 248, it is considered positive. Such a number is greater than 0. To specify that a number is negative, you must type - to its left. An example would be -248. When declaring a variable that would hold a number, you will have the option of specifying that the variable can hold a positive or a negative value.

A variable is referred to as signed when it can hold either a positive or a negative number. The positive sign can be set by either not typing any sign to its left, as in 248, or by adding + to its left, as in +248.

A variable is referred to as unsigned if it must hold only a positive number. If it is given a negative number, in the best case scenario, the compiler would not do anything. In the bad case scenario, the value would be unpredictable. With some compilers, the program would crash.

 Bytes

The most fundamental number in C++/CLI is represented by the Byte data type (this data type is not formally used in C++). A Byte is an unsigned number whose value ranges from 0 to 255. To declare such a variable, you can use the Byte keyword. The Byte data type is defined in the System namespace. Here is an example:

```using namespace System;

int main()
{
Byte Age = 242;

Console::WriteLine(Age);

return 0;
}```

This would produce:

```242
Press any key to continue```

If you want to use a small number but whose variable can hold either positive or negative values, you can declare it using the SByte data type. A variable declared with SByte can hold a value between -127 and 128, not more, not less. The SByte data type is defined in the System namespace. Here is an example:

```using namespace System;

int main()
{
SByte Age = 24;

Console::WriteLine(Age);

return 0;
}```

This would produce:

```24

Press any key to continue```

 Practical Learning: Declaring a Byte Variable
1. To declare a Byte variable, change the file as follows:

 ```using namespace System; int main() { Byte stories = 2; Console::WriteLine("=//= Altair Realty =//="); Console::WriteLine("-=- Properties Inventory -=-"); Console::WriteLine("Property:"); Console::Write("Stories: "); Console::WriteLine(stories); return 0; }```
2. Execute the project to see the result:

 ```=//= Altair Realty =//= -=- Properties Inventory -=- Property: Stories: 2 Press any key to continue . . .```
3. Close the DOS window
 Short Variables

If you need to represent a number that is a little higher than the Byte can hold, you can declare it using the short keyword. Here is an example:

```using namespace System;

int main()
{
short Pages = 424;

Console::WriteLine(Pages);

return 0;
}```

A short variable can hold a natural number whose value ranges from -32768 to 32767. Based on this, it is important to note that a Byte value can fit in a short. Therefore, it is normal to declare as short a variable that would hold even small numbers such as people's ages.

When assigning a value to the variable, you can use a number in that range. If you assign a value out of that range, you would receive a warning and the number would be "truncated". That is, the number would be converted to the value of the other extreme. Consider the following program:

```using namespace System;

int main()
{
short Number = 32769;

Console::WriteLine(Number );

Console::WriteLine();
return 0;
}```

This would produce the following warning:

`warning C4309: 'initializing' : truncation of constant value`

The program would produce:

```-32767

Press any key to continue```

If you want to enforce the idea that the variable is signed, you can type the signed keyword before short. Here is an example:

```using namespace System;

int main()
{
signed short Number = -326;

Console::WriteLine(Number );

Console::WriteLine();
return 0;
}```

If you want the variable to hold only positive numbers, you can type the unsigned keyword to the left of short and make sure you omit the signed keyword. When using the unsigned keyword, you can store numbers that range from 0 to 65535. If you assign either a negative number or a number higher than 65535, the number would be truncated. This time, the truncation is done differently but the result is not as intended. Here is an example:

```using namespace System;

int main()
{
unsigned short Number = -326;

Console::WriteLine(Number );

return 0;
}```

After a warning, this would produce:

```65210
Press any key to continue```

Besides the short keyword, to declare a variable for a relatively small number, you can use the Int16 data type. The Int16 data type is defined in the System namespace. Its variable can hold values in the same range as the short. Here is an example:

```using namespace System;

int main()
{
System::Int16 Number = -326;

Console::WriteLine(Number );

Console::WriteLine();
return 0;
}```

Because an Int16 variable can hold negative values, if you want to use only positive values, you can declare the variable using the UInt16 data type (The U stands for "unsigned"). The UInt16 data type is defined in the System namespace. Here is an example:

```using namespace System;

int main()
{
System::UInt16 Number = 60326;

Console::WriteLine(Number );

Console::WriteLine();
return 0;
}```

This would produce:

```60326

Press any key to continue```
 Integral Variables

In some cases you may need a variable that hold a number larger than the short or the Int16 can carry. To declare such a variable, you can use the int keyword. The int data type is used for a variable whose value can range from –2,147,483,648 to 2,147,484,647. Here is an example:

 ```using namespace System; int main() { int Number = 602; Console::WriteLine(Number); Console::WriteLine(); return 0; }```

Compiled, the program would produce:

 ```602 Press any key to continue```

It is important to note that, based on its range of values, an int variable can hold the same values as the Byte, the short, or the Int16. Therefore, it is perfectly normal and sometimes preferable to use int for a variable intended to hold natural numbers.

By default, an int declared variable can hold either positive or negative values. You can still enforced this by typing the signed keyword to the left of int:

```using namespace System;

int main()
{
signed int Number = 602;

Console::WriteLine(Number);

Console::WriteLine();
return 0;
}```

If you want the variable to hold only positive numbers, you can type the unsigned keyword to its left and omit the signed. Here is an example:

```using namespace System;

int main()
{
unsigned int Number = 46082;

Console::WriteLine(Number);

return 0;
}```

Besides the int keyword, you can use the Int32 data type. An Int32 holds the same range of values as the int. The Int32 data type is defined in the System namespace. Here is an example:

```using namespace System;

int main()
{
System::Int32 Number = 46802;

Console::WriteLine(Number);

Console::WriteLine();
return 0;
}```

To specify that the variable must hold only positive numbers, besides unsigned int, you can use the UInt32 data type to declare the variable. The UInt32 data type is defined in the System namespace. Here is an example:

 ```using namespace System; int main() { UInt32 Number = 46082; Console::WriteLine(Number); return 0; }```

 Practical Learning: Declaring Unsigned Variables
1. To use unsigned variables, change the file as follows:

 ```using namespace System; int main() { Byte stories = 2; unsigned bedrooms = 5; unsigned yearBuilt = 1962; Console::WriteLine("=//= Altair Realty =//="); Console::WriteLine("-=- Properties Inventory -=-"); Console::WriteLine("Property:"); Console::Write("Stories: "); Console::WriteLine(stories); Console::Write("Bedrooms: "); Console::WriteLine(bedrooms); Console::Write("Year Built: "); Console::WriteLine(yearBuilt); return 0; }```
2. Execute the project to see the result:

 ```=//= Altair Realty =//= -=- Properties Inventory -=- Property: Stories: 2 Bedrooms: 5```
3. Close the DOS window
 Long Integers

While the int and the Int32 data types can hold significantly large numbers, you may still need a variable that can hold very large numbers. Such a variable can be declared using the long data type. Such a variable can hold a number that ranges from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Based on this, it is important to note that a long variable can hold a short, an int, an Int16 or an Int32 variable but, unlike the int and the Int32 that are more commonly used, the long is more appropriate if you know that the variable will really need to carry very large numbers. Otherwise, the long can use significant memory.

Here is an example:

```using namespace System;

int main()
{
long Number = 46082;

Console::WriteLine(Number);

return 0;
}```

This would produce:

```46082
Press any key to continue```

When declaring a variable as long, it can hold either positive or negative numbers. You can still enforce this by declaring the variable as signed long.

 Practical Learning: Declaring Long Integers
1. To declare a long variable, change the file as follows:

 ```using namespace System; int main() { long propertyNumber = 490724; Byte stories = 2; unsigned bedrooms = 5; unsigned yearBuilt = 1962; Console::WriteLine("=//= Altair Realty =//="); Console::WriteLine("-=- Properties Inventory -=-"); Console::Write("Property #: "); Console::WriteLine(propertyNumber); Console::Write("Stories: "); Console::WriteLine(stories); Console::Write("Bedrooms: "); Console::WriteLine(bedrooms); Console::Write("Year Built: "); Console::WriteLine(yearBuilt); return 0; }```
2. Execute the project to see the result:

 ```=//= Altair Realty =//= -=- Properties Inventory -=- Property #: 490724 Stories: 2 Bedrooms: 5 Year Built: 1962 Press any key to continue . . .```
3. Close the DOS window
 Initializing an Integral Variable

After declaring an integer variable, you can initialize it with an appropriate value. As far as the value is concerned, you have two main alternatives. You can initialize an integral variable with a decimal value as we have done so far. Alternatively, you can initialize an integral variable with a hexadecimal value.