Home

Using Variables: A Word

 

Introduction

A word is a group of 16 consecutive bits. The bits are counted from right to left starting at 0:

 

Considered as a group of 16 bits, the most right bit of a word, bit 0, is called the least significant bit or Low Order bit or LO bit or LOBIT. The most left bit, bit 15, is called the most significant bit or High Order bit or HI bit or HIBIT. The other bits are referred to using their positions: bit 1, bit 2, bit 3, etc.

Considering that a word is made of two bytes, the group of the right 8 bits is called the least significant byte or Low Order byte or LO byte or LOBYTE. The other group is called the most significant byte or High Order byte or HI byte or HIBYTE.

The representation of a word in binary format is 0000000000000000. To make it easier to read, you can group bits by 4, like this: 0000 0000 0000 0000. Therefore, the minimum binary value represented by a word is 0000 0000 0000 0000. The minimum decimal value of a word is 0. The minimum hexadecimal value you can store in a word is 0x0000000000000000. This is also represented as 0x00000000, or 0x0000, or 0x0. All these numbers produce the same value, which is 0x0.

The maximum binary value represented by a word is 1111 1111 1111 1111. To find out the maximum decimal value of a word, you can use the base 2 formula, filling out each bit with 1:

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

= 32768 + 16384 + 8192 + 4096 + 2048 + 1024 + 512 + 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1

= 65535

To find out the maximum hexadecimal number you can store in a word, replace every group of 4 bits with an f or F:

1111 1111 1111 1111
f f f f
= 0xffff
= 0xFFFF
= 0Xffff
= 0XFFFF
 

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. Since the byte is used to 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. The smallest integer you can store in a word is declared with the short keyword followed by a name. 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 NumericRepresentation
{
	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.

Unsigned Short Integers

If a variable must hold positive and relatively small numbers, it is referred as an unsigned short integer. Such a variable can be declared using the ushort keyword. An unsigned short integer can hold numbers that range from 0 to 65535 and therefore can fit in 16 bits. Here is an example:

using System;

class NumericRepresentation
{
	static void Main()
	{
		// These variables must hold only positive integers
		ushort NumberOfTracks;
		ushort MusicCategory;

		NumberOfTracks = 16;
		MusicCategory  = 2;

		Console.Write("This music album contains ");
		Console.Write(NumberOfTracks);
		Console.WriteLine(" tracks");
		Console.Write("Music Category: ");
		Console.Write(MusicCategory);
		Console.WriteLine();
	}
}

This would produce:

This music album contains 16 tracks
Music Category: 2

Practical LearningPractical Learning: Using Unsigned Short Integers

  1. To use unsigned short integers, change the file as follows:
     
    using System;
    
    class Program
    {
        static void Main()
        {
            byte shirts;
            byte pants;
            ushort otherItems;
    
            shirts     = 4;
            pants      = 0;
            otherItems = 3;
    
            Console.WriteLine("-/- Georgetown Cleaning Services -/-");
            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();
        }
    }
  2. 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 . . .
  3. Close the DOS window
 

Previous Copyright © 2006-2016, FunctionX, Inc. Next