Home

Numeric Values

   

Integers

 

Introduction

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

Word

 

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
 

ApplicationApplication: Introducing Numeric Values

  1. Start Microsoft Visual C++
  2. To create a new application, on the Start Page, click New Project...
  3. If you are using Microsoft Visual Studio, in the left list, click Visual C++.
    In the middle list, click Win32 Console Application
  4. Set the Name to LoanPreparation1
  5. Click OK
  6. In the first page of the wizard, click Next
  7. In the second page, click Console Application and click Empty Project
  8. Click Finish
  9. To create a source to write code, on the main menu, click Project -> Add New Item...
  10. In the middle list, click C++ File (.cpp)
  11. Change the Name to Evaluation
  12. Click Add
  13. In the empty document, type the following:
    #include <iostream>
    using namespace std;
    
    void main()
    {
    	wprintf_s(L"=-= Loan Evaluation =-=");
    	_putwch('\n');
    	
    	wprintf_s(L"=-= Loan Summary =-=");
    	_putwch('\n');
    
    	getchar();
    	getchar();
    }
 

Signed 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 short keyword. To initialize the variable, assign a value between -32768 et 32767. Here is an example:

#include <iostream>
using namespace std;

void main()
{
    short small = 448;
}

To show the value of a short variable, use cout << and type its value after <<. Here is an example:

#include <iostream>
using namespace std;

void main()
{
	short small = 448;

	cout << small;
	putchar(L'\n');
}

This would produce:

448
Press any key to continue . . .

Remember that, to get the value of a variable that holds a (any) number, use cin >> and type the name of the variable after >>.

Since a byte can hold either a positive or a negative number, to indicate that your variable is signed, you can precede the short data type with the signed keyword. Here is an example:

#include <iostream>
using namespace std;

void main()
{
	signed short small = 448;

	cout << small;
	putchar(L'\n');
}

This would produce the same result as before.

To support signed short integers, the Win32 library provides the SHORT data type. You can use it the same way you would the short data type. Here is an example:

#include <iostream>
#include <Windows.h>
using namespace std;

void main()
{
	SHORT number = 1609;

	cout << number;
	putchar(L'\n');
}

This would produce:

1609
Press any key to continue . . .

Unsigned Short Integers

If a variable must hold positive and relatively small numbers, it is referred as an unsigned short integer. To declare a variable that can hold relatively small numbers, use the unsigned short data type. An unsigned short integer can hold numbers that range from 0 to 65535 and therefore can fit in 16 bits. Here are examples:

#include <iostream>
using namespace std;

void main()
{
	unsigned short number = 1609;

	cout << number;
	putchar(L'\n');
}

This would produce:

This music album contains 16 tracks
Music Category: 2

To support unsigned short values, the Win32 library provides the USHORT and the WORD data types. You can use them the same way you would use unsigned short.

ApplicationApplication: Using Unsigned Short Integers

  1. To use unsigned short integers, change the file as follows: 
    #include <iostream>
    using namespace std;
    
    void main()
    {
    	unsigned short period;
    
    	wprintf_s(L"=-= Loan Evaluation =-=");
    	_putwch('\n');
    	wprintf_s(L"Enter the number of months: ");
    	cin >> period;
    	
    	wprintf_s(L"=-= Loan Summary =-=");
    	_putwch('\n');
    	cout << "Period: " << period << " months\n";
    
    	getchar();
    	getchar();
    }
  2. To execute the program, on the main menu, click Debug -> Start Debugging
  3. When asked to enter the number of months, type 36 and press Enter
    =-= Loan Evaluation =-=
    Enter the number of months: 36
    =-= Loan Summary =-=
    Period: 36 months
  4. To close the DOS window, press Enter and return to your programming environment

Signed Integers

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.

Double-Word

The most right bit, bit 0, is called the low order bit or LOBIT. The most left bit, bit 31, is called the high order 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 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 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 LOWORD. The group of the left 16 bits, or the left word, is called the high order 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:

2n-1 230  229  228 227  226 225 224
etc 1,073,741,824 536,870,912 268,435,456 134,217,728 67,108,864 33,554,432 16,777,216

223 222 221 220 219 218 217 216
8,388,608 4,194,304 2,097,152 1,048,576 524,288 262,144 131,072 65,536

215 214 213 212 211 210 29 28
32,768 16,384 8,192 4,096 2,048 1,024 512 256

27 26 25 24 23 22 21 20
128 64 32 16 8 4 2 1

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 word is 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

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 declare a variable that can hold large numbers that require 32 bits of space, you can use the int keyword. The integer data type is used for a variable whose value can range from -2,147,483,648 to 2,147,484,647.

Here is an example:

#include <iostream>

using namespace std;

void main()
{
	int number = 44608;

	cout << number;
	putchar(L'\n');
}

When executed, the program would produce:

44608
Press any key to continue . . .

Since the value can be either positive or negative, to indicate this on the variable, you can use either the signed keyword to declared the variable or you can precede int with signed. Here is an example:

#include <iostream>
using namespace std;

void main()
{
	signed number = 44608;

	cout << number;
	putchar(L'\n');
}

 To support integer variables, the Win32 library provides the INT data type. You can use it the same way you would use the int keyword. Here is an example:

#include <iostream>
#include <Windows.h>
using namespace std;

void main()
{
	INT number = 44608;

	cout << number;
	putchar(L'\n');
}

 The Win32 library provides the INT32 data type for the same goal.

Unsigned Integers

An unsigned integer is a positive number that can be stored in 32 bits. The value must range from 0 to 2,147,484,647. To declared a variable that can handle such as value, you can use either the unsigned keyword by itself or the unsigned int data type. Here is an example:

#include <iostream>
using namespace std;

void main()
{
	unsigned number;
	unsigned int value;
}

To support unsigned integers, the Win32 library provides the UINT and the UINT32 data types. They can be used the same way the unsigned int is.

Practical LearningPractical Learning: Using Integer Variables

  1. To use integer variables, change the source file as follows:
    #include <iostream>
    using namespace std;
    
    void main()
    {
    	unsigned short period;
    	unsigned int   principal;
    
    	wprintf_s(L"=-= Loan Evaluation =-=");
    	_putwch('\n');
    	wprintf_s(L"Enter the amount of the loan: ");
    	cin >> principal;
    	wprintf_s(L"Enter the number of months: ");
    	cin >> period;
    	
    	wprintf_s(L"=-= Loan Summary =-=");
    	_putwch('\n');
    	cout << "Principal: " << principal << "\n";
    	cout << "Period:    " << period << " months\n";
    	
    	getchar();
    	getchar();
    }
  2. To test the project, press F5
  3. When asked for the amount of the loan, type 3600 and press Enter
  4. For the number of months, type 24 and press Enter:
    =-= Loan Evaluation =-=
    Enter the amount of the loan: 3500
    Enter the number of months: 24
    =-= Loan Summary =-=
    Principal: 3500
    Period:    24 months
  5. Return to your programming environment

Long Integerrs

   

Introduction

Besides the int keyword or the INT data type, you can also use the long keyword to declare a variable that would hold natural numbers. Here is an example:

#include <iostream>
using namespace std;

void main()
{
    long number;
}

To support long variables, the Win32 library provides the LONG and the LONG32 data types that can be used in the same circumstances the long keyword would be.

Insigned Long Integers

The long keyword is used for numbers that can be either positive or negative. If you want the variable to hold only positive numbers, declare it using the unsigned long data type. In this case, the values must range from 0 to 4,294,967,295. To support positive long integers, the Win32 library provides the ULONG, the ULONG32, and the DWORD data types. These can be used in place of unsigned long.

Large Integers

A large integer is a natural number that requires more than 32 bits of memory. In this case, the variable will need double the size of 32 bits, which is 64 bits, also called a quad-word.

To let you store a large signed natural number that requires 64 bits, the Win32 library provides the LONGLONG, the LONG64, the DWORD64, and the INT64 data types. The variable for such data types can hold a number between 9223372036854775808 to 9223372036854775807. Here is an example of declaring a DWORD64 variable:

#include <iostream>
#include <Windows.h>
using namespace std;

void main()
{
	DWORD64 number = 514608;

	cout << number;
	putchar(L'\n');
}

If the number you want to use must be positive, you can use the ULONGLONG, the ULONG64, the DWORDLONG, or the UINT64 data types. The number for such a variable can range from 0 to 18446744073709551615.

 
 

Floating-Point Numbers

 

Floating-Point Numbers With Single-Precision

As opposed to an integer, a floating-point number is one that can use a decimal section that represents a fraction of a "whole" (the whole is 1). The Intell processor supports two categories of floating-point numbers.

A floating-point number is said to have single precision if its limited fraction requires a maximum of 7 digits in the fraction area. To declare a variable that can hold such a number, use the float keyword. Here is an example:

#include <iostream>
using namespace std;

void main()
{
	float number;
}

When specifiyinig the value of a float type, you can use a number that appears natural or you can add a fraction part. Here is an example:

#include <iostream>
using namespace std;

void main()
{
    float number = 51.4608;

    cout << number;
    putchar(L'\n');
}

The value of a variable of type float should be able to fit in 4 bytes where it would range from 3.4 x 10-38 to 3.4 x 1038. To support decimal numbers with single precision, the Win32 library provides the FLOAT data type. It can be used wherever the float keyword can be used.

Floating-Point Numbers With Double-Precision

A floating-point number with double-precision is a number that requires more memory space and at the same time provides more precision. Such a number requires double the amount of single precision. This means that a double-precision number must be stored in 64 bits. Its value would range from 1.7 x 10-308 to 1.7 x 10308.

To declare a variable that must store a large decimal number, use the double keyword. As done for the single-precision variable, when initializing the variable, provide a natural number or a number with a fractional part. This time, you are allowed to provide as long a fractional part as you want. Here is an example:

#include <iostream>
using namespace std;

void main()
{
	double number = 57.4038;

	cout << number;
	putchar(L'\n');
}

To support floating-point numbers with double-precision, the Win32 library provides the DOUBLE data type. It can be used wherever a double keyword would.

Practical LearningPractical Learning: Using Double-Precision Numbers

  1. To use floating-point numbers, change the source file as follows:
    #include <iostream>
    using namespace std;
    
    void main()
    {
    	unsigned short period;
    	unsigned int principal;
    	double interestRate;
    	double interestPercentage;
    	double interestEarned, futureValue;
    	double periodInYears;	
    
    	wprintf_s(L"=-= Loan Evaluation =-=");
    	_putwch('\n');
    	wprintf_s(L"Enter the amount of the loan: ");
    	cin >> principal;
    	wprintf_s(L"Enter the number of months: ");
    	cin >> period;
    	wprintf_s(L"Enter the interest rate applied on the loan: ");
    	cin >> interestRate;
    	
    	periodInYears = period / 12;
    	interestPercentage = interestRate  / 100;
    	interestEarned = principal * interestPercentage * periodInYears;
    	futureValue = principal + interestEarned;
    
    	wprintf_s(L"=-= Loan Summary =-=");
    	_putwch('\n');
    	cout << "Principal:       " << principal << "\n";
    	cout << "Period:          " << period << " months\n";
    	cout << "Interest Rate:   " << interestRate << "%\n";
    	cout << "Interest Earned: " << interestEarned << "\n";
    	cout << "Future Value:    " << futureValue << "\n";
    	
    	getchar();
    	getchar();
    }
  2. To execute, press F5
  3. For the principal, type 2650 and press Enter
  4. For the period, type 28 and press Enter
  5. For the interest rate, type 12.65 and press Enter  
    =-= Loan Evaluation =-=
    Enter the amount of the loan: 2650
    Enter the number of months: 28
    Enter the interest rate applied on the loan: 12.65
    =-= Loan Summary =-=
    Principal:       2650
    Period:          28 months
    Interest Rate:   12.65%
    Interest Earned: 670.45
    Future Value:    3320.45
  6. Press Enter to close the window and return to your programming environment

Long Double-Precision Numbers

For an even larger variable, use the 10 Byte real data type identified as a long double data type that ranges from 3.4 x 10-4932 to 1.1 x 104932.

Here is an example that uses the long double data type:

#include <iostream>
using namespace std;

void main()
{
	long double radius = 15.625, Radius = 18.125;
	long double area, Area, TotalArea;

	Area = Radius * Radius * 3.14159;
	area = radius * radius * 3.14159;
	TotalArea = Area - area;

	cout << "Properties of the plate";
	cout << "\nExternal Radius: " << Radius;
	cout << "\nInternal Radius: " << radius;
	cout << "\nArea: " << TotalArea;
}

Value Casting

 

Introduction

We have been introduced to declaring variables using specific data types. After declaring a value and initializing it, you may want the value to change type without redefining it. This is required in some cases where you already have a value, probably produced by one variable, while another variable declared with a different data type must use the value. This means that you would need to cast a value from one type into another type. For example, you may have declared a variable using a double data type but you need the value of that variable to be used as an int. Transferring a value from one type to another is referred to as casting.

There are two broad types of casting available in C++: C's casting and the C++ standards.

C How To Cast a Value

C, the parent of C++ supports value casting by specifying the type of value you want an existing one to have. To do this, you use the following formula:

(DataType)Expression

Based on this formula, in the parentheses, enter the type of data you want the existing or resulting value to have. The DataType factor can be any of the data types we saw above. The Expression factor can be a constant value. Here is an example:

#include <iostream>
using namespace std;

int main()
{
	cout << "Number: " << (int)3.14159 << "\n";

	return 0;
}

Notice that the value to cast is a floating-point number. If the casting succeeds, the new value would be conform to the type in parentheses. For example, the above code would produce:

Number: 3

In the above code, we cast the value directly where it is need. Otherwise, you can first declare the variable that will hold the value. Here is an example:

#include <iostream>
using namespace std;

int main()
{
	int number;

	number = (int)3.14159;

	cout << "Number: " << number << "\n";

	return 0;
}

The Expression can also be the result of a calculation. In this case, you should include the whole expression is its own parentheses. The Expression factor of our formula can also be the name of a variable that holds a value. Here is an example:

#include <iostream>
using namespace std;

int main()
{
	double price = 258.85;
	int number;

	cout << "Price? $" << price << "\n";

	number = (int)price;

	cout << "Number: " << number << "\n";

	return 0;
}

This would produce:

Price? $258.85

Number: 258

C++ Casting

C++ provides its own support of value casting using various keywords so you can specify the type of casting you want. One of the keywords used is static_cast and the formula is:

static_cast<DataType>(Expression)

In this formula, the static_cast keyword, the <, the >, and the parentheses are required. The DataType should be an existing data type. The Expression can be a constant value. Here is an example that casts a floating-point number to an integer:

#include <iostream>
using namespace std;

int main()
{
	cout << "Number: " << static_cast<int>(3.14159) << "\n";

	return 0;
}

You can also assign the resulting value to a previously declared variable:

#include <iostream>
using namespace std;

int main()
{
	int number = static_cast<int>(3.14159);

	cout << "Number: " << number << "\n";

	return 0;

}

The value to cast can also be the result of a calculation. The value can also be originating from an existing variable whose value you want to convert to a new type. Here is an example:

#include <iostream>
using namespace std;

int main()
{
	double PI = 3.14159;
	int number;

	number = static_cast<int>(PI);

	cout << "PI = " << PI << endl;
	cout << "Number = " << number << "\n";

	return 0;

}

This would produce:

PI = 3.14159

Number = 3

Variable Scope

 

Introduction

So far, to declare a variable, we use the body of main. Such a variable could be used only in that body. In some cases, you may want to declare a variable that can be accessed from one section of the code. The section of code in which a variable can be accessed is referred to as its scope.

Local Variables

If you declare a variable inside of curly brackets (an opening curly bracket "{" and a closing curly bracket "}"), that variable can be only in that body. Consider the following example:

#include <iostream>
using namespace std;

int main()
{
	double number = 3.14159;

	cout << "Number = " << number << "\n";

	return 0;
}

Such a variable is referred to as local. Everything between these brackets belongs to a local scope. Once you have declared such a variable, you cannot declare another variable in the same scope and that bears the same name. Consider the following example:

#include <iostream>
using namespace std;

int main()
{
	double number = 3.14159;

	cout << "Number = " << number << "\n";

	double number = 2.98;

	cout << "Number = " << number << "\n";

	return 0;
}

This would produce a "redefinition" error and the program would not compile, even if the second declaration uses a different data type. As one type of solution to this kind of problem, C++ allows you to create a "physical" scope. To do this, you can use curly brackets to delimit the scope of a particular variable. Here is an example:

#include <iostream>
using namespace std;

int main()
{
    {
	double number = 3.14159;

	cout << "Number = " << number << "\n";
    }

    double number = 2.98;

    cout << "Number = " << number << "\n";
    return 0;
}

This would produce:

Number = 3.14159

Number = 2.98

In the code, notice that we delimit only the first declaration. Indeed, you can delimit each scope if you want:

#include <iostream>
using namespace std;

int main()
{
    {
	double number = 3.14159;

	cout << "Number = " << number << "\n";
    }	

    {
	double number = 2.98;

	cout << "Number = " << number << "\n";
    }

    return 0;
}
     

Global Variables

C++ allows you to declare a variable outside of any "body". Such a variable is called a global variable. Here is an example:

#include <iostream>
using namespace std;

double number;

int main()
{
	return 0;
}
Author Note C++ Note

Some languages such as Pascal or Visual Basic support global variables. Some other languages such as Java and C# don't support them.

After declaring the variable, you can initialize it immediately:

#include <iostream>
using namespace std;

double number = 3.14159;

int main()
{
    cout << "Number = " << number << "\n";

    return 0;
}

You can also initialize it inside another "body" that would use it. After declaring and initializing the variable, you can access its value and use it. For example, you can display its value to the user. Here is an example:

#include <iostream>
using namespace std;

double number;

int main()
{
    number = 3.14159;

    cout << "Number = " << number << "\n";

    return 0;
}

In C++ (some other languages don't have this situation), the compiler always proceed in a top-down approach. After declaring a variable, only the sections under it can access it. This means that you cannot access a variable above its declaration. Consider the following program where only the area of the declaration has changed:

#include <iostream>
using namespace std;

int main()
{
	number = 3.14159;

	cout << "Number = " << number << "\n";

	return 0;
}

double number;

This program would not compile. The reason is that, when accessing it inside of main(), as far as main() is concerned, the variable has never been declared and C++ doesn't allow to use a variable before it has been declared. Therefore, you must always declare a variable before accessing it.

 
 
   
 

Previous Copyright © 2010 FunctionX, Inc. Next