Short Integers |
A word, which is a group of 16 contiguous bits or 2 bytes, can be used to hold a natural number. As we have studied, the maximum numeric value that can fit in a word is 65535. To declare a variable for such a value, you can use the var keyword and initialize the variable with a value between –32768 to 32767. Here is an example:
using System;
class Exercise
{
static void Main()
{
var SchoolEffective = 1400; // Number of Students
Console.Write("School Effective: ");
Console.WriteLine(SchoolEffective);
}
}
This would produce:
School Effective: 1400
Press any key to continue . . .
Since the byte is used for characters and very small numbers, whenever you plan to use a number in your program, the minimum representation you should use is a word.
A natural number is also called an integer. If you want to declare the variable using a data type, the smallest integer you can store in a word is declared with the short keyword. Because a short integer is signed by default, it can store a value that ranges from –32768 to 32767. Here is an example program that uses two short integers:
using System;
class Exercise
{
static void Main()
{
short NumberOfPages;
short Temperature;
NumberOfPages = 842;
Temperature = -1544;
Console.Write("Number of Pages of the book: ");
Console.WriteLine(NumberOfPages);
Console.Write("Temperature to reach during the experiment: ");
Console.Write(Temperature);
Console.WriteLine(" degrees\n");
}
}
This would produce:
Number of Pages of the book: 842
Temperature to reach during the experiment: -1544 degrees
Because a short integer handles numbers that are larger than the signed byte, any variable you can declare for a signed byte can also be declared for a short variable.
No comments:
Post a Comment