Subnet Masks: Tutorial

Preface

Remembering back to when I took my first networking class, subnet masks were the hardest concept for me to grasp. I threw together a quick tutorial that I hope is easy to understand and can help you through this concept. The first thing you need to understand before even looking at subnet-masking is how to read and manipulate binary. Here is a quick youtube tutorial Binary Conversion Tutorial Once, You have a grasp on converting Decimal into Binary, we can move forward.

Anatomy of a Subnet Mask

A subnet mask goes hand in hand with an IP address. It is responsible for telling the computer which portion of an IP address is the network address, and which portion (bits) are reserved for the hosts. It has the same "dotted-decimal" notation, a very common example of an IP address and subnet mask in this format would be: (IP)192.168.1.0 (mask)255.255.255.0 The basic structure of this format is a 4-byte address (32-bits), which means there are 32 binary digits (1's and 0's) that make up the address. Translating the mask (255.255.255.0) into binary would look like this: 11111111.11111111.11111111.00000000 Note: The mask's leftmost bits must be 1's and the rightmost bits must be 0's( Another way of writing the subnet mask is in CIDR notation, which is basically counting the number of active bits (1's) from the left. So a mask of 255.255.255.0 in CIDR notation would be /24. Applying this to the IP address (192.168.1.0), we see that the mask (255.255.255.0) has the first 3 octet's (8 bit sets) are all set to 1's, meaning that the first 3 bytes of the IP address are the network address, and the last byte of the IP address is reserved for host addresses. Thus we can conclude that: Network address: 192.168.1.0 Host Address Range: 192.168.1.1 - 192.168.1.255 Note: Host the first and last addresses are reserved for gateway and loopback addresses i.e.(192.168.1.1 and 192.168.1.255) Here is a handy chart that allows you to quickly translate an octet into its binary equivalent: quickReference

Subnetting

Subnetting is the act of breaking down the host portion of an IP address to separate the distribution of host addresses into separate zones or subnets, within the network. When looking at the right-most octet of a subnet mask we can tell how many subnets are present, and how many hosts per subnet.
 Content Is not completed yet, 3/19/13 

C++ – Relative Alignment of String Output

I was doing a project for my C++ class and was trying to find
an easy solution to print a string to the console and after that
string, I was to print "Record Number: " and the corresponding
number. The problem was that I needed the string to be at an 
absolute position from the left side of the screen. I read various 
blogs trying to find a simple way to do what I desired but was 
unsuccessful (doesn't mean it's not out there). The reasoning I 
was given why setw(25) wasn't working is because it was a string
object. So I developed this work-around, I am no professional
and I'm sure that someone will tell me there is a far simpler way,
but this is how I solved that problem.
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;


int main()
{
	int location;
	int indent;
	bool done = false;
	// create an array of 4 names
	string names[4] = {"Chad", "Mitch", "Abbie", "Courtney"};

	do
	{
		// prompt the user for an index location
		cout << "\nEnter Record number(1-4) or 0 to quit: ";
		cin >> location;

		// input validation
		if (location == 0)
		{
			done = true;
		}
		else if (location < 0 || location > 4)
		{
			cout << "\nYou must enter a number between 1 and 4\n";
		}
		else
		{
			// Prompt for absolute positioning
			cout << "How far from the left would you like to indent: ";
			cin >> indent;

			// Store an isolated instance of the array at index "location"
			string result = names[location - 1];
			            
			// Add spaces after result until desired absolute spacing achieved
			for (int i = result.length(); i < indent; i++)
			{
			   result += " ";
			}

			result += "Record Number: ";

		   // Print formatted text
			cout << endl << result << location + 1 << endl;
		}	
	}
	while(!done);

	return 0;
}