Friday, May 2, 2008

Using Variables in c#

Using Variables in c#:::

A Double-Word


Introduction


A double-word is a group of two consecutive Words. This means that a double-word combines 4 bytes or 32 bits. The bits, counted from right to left, start at 0 and end at 31.

The most right bit, bit 0, is called the Low Order bit or LO bit or LOBIT. The most left bit, bit 31, is called the High Order bit or HI bit or HIBIT. The other bits are called using their positions.

The group of the first 8 bits (from bit 0 to bit 7), which is the right byte, is called the Low Order Byte, or LO Byte. It is sometimes referred to as LOBYTE. The group of the last 8 bits (from bit 24 to bit 31), which is the left byte, is called the High Order Byte, or HI Byte or HIBYTE. The other bytes are called by their positions.

The group of the right 16 bits, or the right Word, is called the Low Order Word, or LO Word, or LOWORD. The group of the left 16 bits, or the left Word, is called the High Order Word, or HI Word, or HIWORD.

The minimum binary number you can represent with a double-word is 0. The minimum decimal value of a double-word is 0. To find out the maximum decimal value of a word, you can use the base 2 formula giving a 1 value to each bit:



1*231+1*230+1*229 + 1*228 + 1*227 + 1*226 + 1*225 + 1*224 + 1*223 + 1*222 + 1*221 + 1*220 + 1*219 + 1*218 + 1*217 + 1*216 + 1*215 + 1*214 + 1*213 + 1*212 + 1*211 + 1*210 + 1*29 + 1*28 + 1*27 + 1*26 + 1*25 + 1*24 + 1*23 + 1*22 + 1*21 + 1*20

= 2,147,483,648 + 1,073,741,824 + 536,870,912 + 268,435,456 + 134,217,728 + 67,108,864 + 33,554,432 + 16,777,216 + 8,388,608 + 4,194,304 + 2,097,152 + 1,048,576 + 524,288 + 262,144 + 131,072 + 65,536 + 32,768 + 16,384 + 8,192 + 4,096 + 2,048 + 1,024 + 512 + 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1

= 4,286,578,708

The minimum hexadecimal value you can store in a double-word is 0x00000000000000000000000000000000 which is the same as 0x0. To find out the maximum hexadecimal number you can represent with a word, replace every group of 4-bits with an f or F:

1111 1111 1111 1111 1111 1111 1111 1111
f f f f f f f f
= 0xffffffff = 0Xffffffff = 0XFFFFFFFF = 0xFFFFFFFF

To declare a variable that can hold large values, you can use the var keyword and initialize the variable with the desired value. Here is an example:

using System;

class Exercise
{
static void Main()
{
var Population = 72394475;

Console.Write("Country Population: ");
Console.WriteLine(Population);
}
}
This would produce:

Country Population: 72394475
Press any key to continue . . .
Practical Learning: Using Unsigned Integers



Start Microsoft Visual C#
To create a new application, on the main menu, click File -> New -> Project...
In the Templates section, click Console Application
Change the Name to GeorgetownCleaningServices3 and click OK
Change the file as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GeorgetownCleaningServices3
{
class Program
{
static void Main(string[] args)
{
byte Shirts;
byte Pants;
ushort OtherItems;

Shirts = 4;
Pants = 0;
OtherItems = 3;

Console.WriteLine("-/- Georgetown Cleaning Services -/-");
Console.WriteLine("========================");
Console.WriteLine("------------------------");
Console.WriteLine("Item Type Qty");
Console.WriteLine("------------------------");
Console.Write("Shirts ");
Console.WriteLine(Shirts);
Console.Write("Pants ");
Console.WriteLine(Pants);
Console.Write("Other Items ");
Console.WriteLine(OtherItems);
Console.WriteLine("========================");
Console.WriteLine();
}
}
}


Execute the program to see the result
Close the DOS window
Signed Integers



A double-word is large enough to contain double the amount of data that can be stored in a word. This is equivalent to 32 bits or 4 bytes or 4,294,967,295. Therefore, a double-word is used for large numbers that would not fit in a word.

To use a variable that would hold quite large numbers, besides the var keyword, you can declare it using the int keyword. A variable declared as int can store values between –2,147,483,648 and 2,147,484,647 negative or positive, that can fit in 32 bits.

Here is an example:

using System;

class Exercise
{
static void Main()
{
int CoordX;
int CoordY;

CoordX = 12;
CoordY = -8;

Console.Write("Cartesian Coordinate System: ");
Console.Write("P(");
Console.Write(CoordX);
Console.Write(", ");
Console.Write(CoordY);
Console.WriteLine(")\n");
}
}
When executed, the program would produce:

Cartesian Coordinate System: P(12, -8)
If you declare an integer variable using the var keyword and initialize it with a value lower than 2,147,484,647, the compiler concludes that the memory needed to store that variable is 32 bits:



When initializing an integral variable, instead of a decimal number, you can also initialize it with a hexadecimal value whose decimal equivalent is less than 2,147,484,647. Here is an example:

using System;

class Exercise
{
static void Main()
{
var Number = 0xF0488EA;

Console.Write("Number: ");
Console.WriteLine(Number);
}
}
This would produce:

Number: 251955434
Press any key to continue . . .
Unsigned Integers



If the variable must hold only positive natural numbers, you can declared it using the uint keyword. The uint keyword is used to identify a 32-bit positive integer whose value would range from 0 to 2,147,484,647. Here is an example:

using System;

class Exercise
{
static void Main()
{
uint DayOfBirth;
uint MonthOfBirth;
uint YearOfBirth;

DayOfBirth = 8;
MonthOfBirth = 11;
YearOfBirth = 1996;

Console.WriteLine("Red Oak High School");
Console.Write("Student Date of Birth: ");
Console.Write(MonthOfBirth);
Console.Write("/");
Console.Write(DayOfBirth);
Console.Write("/");
Console.Write(YearOfBirth);
Console.WriteLine();
}
}
This would produce:

Red Oak High School
Student Date of Birth: 11/8/1996
Practical Learning: Using Unsigned Integers



To use unsigned variables, change the file as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GeorgetownCleaningServices3
{
class Program
{
static void Main(string[] args)
{
byte Shirts;
byte Pants;
ushort OtherItems;

uint OrderDay;
uint OrderMonth;
uint OrderYear;

Shirts = 4;
Pants = 0;
OtherItems = 3;
OrderDay = 15;
OrderMonth = 7;
OrderYear = 2002;

Console.WriteLine("-/- Georgetown Cleaning Services -/-");
Console.WriteLine("========================");
Console.Write("Order Date: ");
Console.Write(OrderMonth);
Console.Write('/');
Console.Write(OrderDay);
Console.Write('/');
Console.WriteLine(OrderYear);
Console.WriteLine("------------------------");
Console.WriteLine("Item Type Qty");
Console.WriteLine("------------------------");
Console.Write("Shirts ");
Console.WriteLine(Shirts);
Console.Write("Pants ");
Console.WriteLine(Pants);
Console.Write("Other Items ");
Console.WriteLine(OtherItems);
Console.WriteLine("========================");
Console.WriteLine();
}
}
}


Execute the program. This would produce:
-/- Georgetown Cleaning Services -/-
========================
Order Date: 7/15/2002
------------------------
Item Type Qty
------------------------
Shirts 4
Pants 0
Other Items 3
========================

Press any key to continue . . .


Close the DOS window
A Quad-Word


Introduction



Sometimes you may want to store values that a double-word cannot handle. To store a very large number in a variable, you can consider a combination of 64 bits. The group can also be referred to as a quad-word. A quad-word is so large it can store numbers in the range of –9,223,372,036,854,775,808 and 9,223,372,036,854,775,807.

If you declare an integer variable using the var keyword and initialize it with a value between 2,147,484,647 and 9,223,372,036,854,775,807, the compiler concludes that the memory needed to store that variable is 64 bits:



Long Integers



If you want to use a variable that can hold very large numbers that would require up to 64 bits, you can declare it using either the var or the long keyword.

In C++, the long data type is 32 bits while in C#, the long data type is 64 bits.

As stated previously, if you initialize the variable with a value lower than 2,147,484,647, the compiler would allocate 32 bits of memory for it. If you initialize the variable with a value between 2,147,484,647 and 9,223,372,036,854,775,807, the compiler would allocate 64 bits of memory for it. If the value is higher than 9,223,372,036,854,775,807, which is too large, the compiler would present an error:



This means that you should limit the values assigned to integral variables to 64 bits, which is very significant.

Here is an example that uses the long data type:

using System;

class Exercise
{
static void Main()
{
var CountryArea = 5638648;

Console.Write("Country Area: ");
Console.Write(CountryArea);
Console.Write("km2\n");
}
}
This would produce:

Country Area: 5638648km2
Press any key to continue . . .
As mentioned for other integral types, you can initialize a long variable with a hexadecimal value.

Although the long data type is used for large number, it mainly indicates the amount of space available but you do not have to use the whole space. For example, you can use the long keyword to declare a variable that would hold the same range of numbers as the short, the int, or the uint data types. If you declare a variable as long but use it for small numbers that don't require 64 bits, the compiler would allocate the appropriate amount of space to accommodate the values of the variable. Consequently, the amount of space made available may not be as large as 64 bits. If you insist and want the compiler to reserve 64 bits, when assigning a value to the variable, add an L suffix to it. Here is an example that uses space of a long data type to store a number that would fit in 32 bits:

using System;

class NumericRepresentation
{
static void Main()
{
long CountryArea;

CountryArea = 5638648L;

Console.Write("Country Area: ");
Console.Write(CountryArea);
Console.Write("km2\n");
}
}
Therefore, keep in mind that an int, a uint, a short, or a ushort can fit in a long variable.

Unsigned Long Integers



You can use a combination of 64 bits to store positive or negative integers. In some cases, you will need a variable to hold only positive, though large, numbers. To declare such a variable, you can use the ulong data type. A variable declared as ulong can handle extremely positive numbers that range from 0 to 18,446,744,073,709,551,615 to fit in 64 bits.

No comments: