Subscribe

RSS Feed (xml)

C# Integers

C# defines nine integer types: char, byte, sbyte, short, ushort, int, uint, long, and ulong. However, the char type is primarily used for representing characters, and it is discussed later in this chapter. The remaining eight integer types are used for numeric calculations. Their bit-width and ranges are shown here:

Type

Width in Bits

Range

byte

8

0 to 255

sbyte

8

128 to 127

short

16

32,768 to 32,767

ushort

16

0 to 65,535

int

32

2,147,483,648 to 2,147,483,647

uint

32

0 to 4,294,967,295

long

64

9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

ulong

64

0 to 18,446,744,073,709,551,615

As the table shows, C# defines both signed and unsigned versions of the various integer types. The difference between signed and unsigned integers is in the way the high-order bit of the integer is interpreted. If a signed integer is specified, then the C# compiler will generate code that assumes that the high-order bit of an integer is to be used as a sign flag. If the sign flag is 0, then the number is positive; if it is 1, then the number is negative. Negative numbers are almost always represented using the two’s complement approach. In this method, all bits in the negative number are reversed, and then 1 is added to this number.

Signed integers are important for a great many algorithms, but they have only half the absolute magnitude of their unsigned relatives. For example, as a short, here is 32,767:

0 1 1 1 1 1 1 1  1 1 1 1 1 1 1 1

For a signed value, if the high-order bit were set to 1, the number would then be interpreted as 1 (assuming the two’s complement format). However, if you declared this to be a ushort, then when the high-order bit was set to 1, the number would become 65,535.

Probably the most commonly used integer type is int. Variables of type int are often employed to control loops, to index arrays, and for general-purpose integer math. When you need an integer that has a range greater than int, you have many options. If the value you want to store is unsigned, you can use uint. For large signed values, use long. For large unsigned values, use ulong. For example, here is a program that computes the distance from the Earth to the sun, in inches. Because this value is so large, the program uses a long variable to hold it.

// Compute the distance from the Earth to the sun, in inches.

using System;

class Inches {
 public static void Main() {
   long inches;
   long miles;

   miles = 93000000; // 93,000,000 miles to the sun

   // 5,280 feet in a mile, 12 inches in a foot
   inches = miles * 5280 * 12;

   Console.WriteLine("Distance to the sun: " +
                     inches + " inches.");

 }
}

Here is the output from the program:

Distance to the sun: 5892480000000 inches.

Clearly, the result could not have been held in an int or uint variable.

The smallest integer types are byte and sbyte. The byte type is an unsigned value between 0 and 255. Variables of type byte are especially useful when working with raw binary data, such as a byte stream of data produced by some device. For small signed integers, use sbyte. Here is an example that uses a variable of type byte to control a for loop that produces the summation of the number 100:

// Use byte.

using System;

class Use_byte {
 public static void Main() {
   byte x;
   int sum;

   sum = 0;
   for(x = 1; x <= 100; x++)      sum = sum + x;     Console.WriteLine("Summation of 100 is " + sum);  } }

The output from the program is shown here:

Summation of 100 is 5050

Since the for loop runs only from 0 to 100, which is well within the range of a byte, there is no need to use a larger type variable to control it.

When you need an integer that is larger than a byte or sbyte, but smaller than an int or uint, use short or ushort.

No comments:

Post a Comment

LocalsAdda.com-Variety In Web World

Fun Mail - Fun in the Mail