Friday, May 9, 2008

Introduction to Variables:The Numeric Systems

Introduction


When a computer boots, it “loads” the operating system. If you want to use a program, you must find it either on the Start menu or from its directory and take the necessary action to open it. Such a program uses numbers, characters, meaningful words, pictures, graphics, etc, that are part of the program. As these things are numerous, so is the size of the program, and so is the length of time needed to come up. Your job as a programmer is to create such programs and make them available to the computer, then to people who want to interact with the machine.

To write your programs, you will be using alphabetic letters that are a, b, c, d, e, f, g, h, I, j, k, l, m, n, o, p, q, r, s, t, v, w, x, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z. You will also use numeric symbols 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. Additionally, you will use characters that are not easily readable but are part of the common language; they are ` ~ ! @ # $ % ^ & * ( ) _ + - = : “ < > ; ‘ , . /. Some of these symbols are used in the C# language while some others are not. When creating your programs, you will be combining letters and/or symbols to create English words or language instructions.

Some of the instructions you will give to the computer could consist of counting the number of oranges, converting water to soup, or making sure that a date occurs after January 15. After typing an instruction, the compiler would translate it to machine language. The computer represents any of your instructions as a group of numbers. Even if you ask the computer to use an orange, it would translate it into a set of numbers. As you give more instructions or create more words, the computer stores them in its memory using a certain amount of space for each instruction or each item you use.


There are three numeric systems that will be involved in your programs, with or without your intervention.

The Binary System



When dealing with assignments, the computer considers a piece of information to be true or to be false. To evaluate such a piece, it uses two symbols: 0 and 1. When a piece of information is true, the computer gives it a value of 1; otherwise, its value is 0. Therefore, the system that the computer recognizes and uses is made of two symbols: 0 and 1. As the information in your computer is greater than a simple piece, the computer combines 0s and 1s to produce all sorts of numbers. Examples of such numbers are 1, 100, 1011, or 1101111011. Therefore, because this technique uses only two symbols, it is called the binary system.

When reading a binary number such as 1101, you should not pronounce "One Thousand One Hundred And 1", because such a reading is not accurate. Instead, you should pronounce 1 as One and 0 as zero or o. 1101 should be pronounced One One Zero One, or One One o One.

The sequence of the symbols of the binary system depends on the number that needs to be represented.

The Decimal System



The numeric system that we are familiar with uses ten symbols that are 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. Each of these symbols is called a digit. Using a combination of these digits, you can display numeric values of any kind, such as 240, 3826 or 234523. This system of representing numeric values is called the decimal system because it is based on 10 digits.

When a number starts with 0, a calculator or a computer ignores the 0. Consequently, 0248 is the same as 248; 030426 is the same as 30426. From now on, we will represent a numeric value in the decimal system without starting with 0: this will reduce, if not eliminate, any confusion.

Decimal Values: 3849, 279, 917293, 39473
Non- Decimal Values: 0237, 0276382, k2783, R3273

The decimal system is said to use a base 10. This allows you to recognize and be able to read any number. The system works in increments of 0, 10, 100, 1000, 10000, and up. In the decimal system, 0 is 0*100 (= 0*1, which is 0); 1 is 1*100 (=1*1, which is 1); 2 is 2*100 (=2*1, which is 2), and 9 is 9*100 (= 9*1, which is 9). Between 10 and 99, a number is represented by left-digit * 101 + right-digit * 100. For example, 32 = 3*101 + 2*100 = 3*10 + 2*1 = 30 + 2 = 32. In the same way, 85 = 8*101 + 5*100 = 8*10 + 5*1 = 80 + 5 = 85. Using the same logic, you can get any number in the decimal system. Examples are:

2751 = 2*103 + 7*102 + 5*101 + 1*100 = 2*1000 + 7*100 + 5*10 + 1 = 2000 + 700 + 50 + 1 = 2751

67048 = 6*104 + 7*103 + 0*102 + 4*101 + 8*100 = 6*10000 + 7*1000+0*100+4*10+8*1 = 67048

Another way you can represent this is by using the following table:

etc Add 0 to the preceding value 1000000 100000 10000 1000 100 10 0

When these numbers get large, they become difficult to read; an example is 279174394327. To make this easier to read, you can separate each thousand fraction with a comma. Our number would become 279,174,394,327. You can do this only on paper, never in a program: the compiler would not understand the comma(s).

The Hexadecimal System



While the decimal system uses 10 digits (they are all numeric), the hexadecimal system uses sixteen (16) symbols to represent a number. Since the family of Latin languages consists of only 10 digits, we cannot make up new ones. To compensate for this, the hexadecimal system uses alphabetic characters. After counting from 0 to 9, the system uses letters until it gets 16 different values. The letters used are a, b, c, d, e, and f, or their uppercase equivalents A, B, C, D, E, and F. The hexadecimal system counts as follows: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, and f; or 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. To produce a hexadecimal number, you use a combination of these sixteen symbols.

Examples of hexadecimal numbers are 293, 0, df, a37, c23b34, or ffed54. At first glance, the decimal representation of 8024 and the hexadecimal representation of 8024 are the same. Also, when you see fed, is it a name of a federal agency or a hexadecimal number? Does CAB represent a taxi, a social organization, or a hexadecimal number?

From now on, to express the difference between a decimal number and a hexadecimal one, each hexadecimal number will start with 0x or 0X. The number will be followed by a valid hexadecimal combination. The letter can be in uppercase or lowercase.
Legal Hexadecimals: 0x273, 0xfeaa, 0Xfe3, 0x35FD, 0x32F4e
Non-Hex Numbers: 0686, ffekj, 87fe6y, 312


There is also the octal system but we will not use it anywhere in our applications.

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.