Samker's Computer Forum - SCforum.info

World TOP Headlines: => Around the Web => Topic started by: Samker on 17. October 2008., 19:11:47

Title: How to calculate the Subnet Mask ?
Post by: Samker on 17. October 2008., 19:11:47
What is an IP address ?

An IP address is a number coded on 32 bits (32 "0" or "1") which represents a unique ID for a host on an IP Network. IP networks are the most popular kind of networks. The most famous IP network being Internet !

The IP address is often represented with 4 bytes (or octets) composed by 8 bits each. Each byte is written in decimal notation, in the range 0 to 255, separated by dots. That's what we call the "dotted decimal" notation.

For example, the IP address: 192. 10. 15. 1
Is in fact in binary : 11000000.00001010.00001111.00000001 (see "How to convert decimal to binary ?")

IP addresses must be unique on a same IP network, however, they might be redundant on separate ones.
On Internet, which is a Word Wild IP network, all IP addresses must be unique. It is a little bit like a postal address: if you want to receive your mail, your house number, street and postcode must be unique.
The organisation in charge of allocating IP addresses on Internet is the Internet Assigned Numbers Authority (IANA).
Note that some address ranges, defined in RFC 1597 and RFC 1918 are reserved for private network and are not routed on Internet (namely 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16).

IP adresses are composed of two parts: the "Network" and the "Host" (also called "Node").The Class of the IP address determine which part of the address codes the Network and which part codes the Host (see "What is an IP Class ?").

What is an IP Class ?
.
An IP Class is a range of IP addresses. They have been designed to provide an address sheme which accomodates small and large networks.
.
There are 5 Classes from A to E defined as follows:
Class A - first byte is 1 to 126 decimal (0xxxxxxx in binary)
Class B - first byte is 128 to 191 decimal (10xxxxxx in binary)
Class C - first byte is 192 to 223 decimal (110xxxxx in binary)
Class D - first byte is 224 to 239 decimal (1110xxxx in binary)
Class E - first byte is 240 to 254 decimal (1111xxxx in binary)
.
IANA allocates only Class A to Class C addresses. Class D is reserved for multicast and Class E for future use.
Adresses begining by 127 (01111111) are reserved for loopback or local testing. The IP 127.0.0.1 is usually implemented as local host loopback IP.
.
For each IP Class we also define a Network Mask as follows:
Class A - 255.0.0.0 (11111111.00000000.00000000.00000000 in binary)
Class B - 255.255.0.0 (11111111.11111111.00000000.00000000 in binary)
Class C - 255.255.255.0 (11111111.11111111.11111111.00000000 in binary)
.
The Network part of the IP address is deduced by operating a binary AND between the IP address and the Network Mask of the Class. The binary AND will let "as-is" all binary values combined with "1"s and will zero all others (see "Binary operators").
For example, for Class A:
IP address : aaaaaaaa.bbbbbbbb.cccccccc.dddddddd
Network mask: 11111111.00000000.00000000.00000000
Binary AND --------------------------------------
Network : aaaaaaaa.00000000.00000000.00000000
.
The Host part of the IP address is deduced by operating a binary AND between the IP address and the Network Mask of the Class inversed (binary NOT).
For example, for the same Class A:
IP address : aaaaaaaa.bbbbbbbb.cccccccc.dddddddd
Network mask inversed: 00000000.11111111.11111111.11111111
Binary AND --------------------------------------
Host : 00000000.bbbbbbbb.cccccccc.dddddddd
.
From this comes:
Class A - the first byte codes the Network ("n"s) and the 3 last bytes are the Host ("h"s) : nnnnnnnn.hhhhhhhh.hhhhhhhh.hhhhhhhh
Class B - the fist two bytes codes the Network ("n"s) and the 2 last bytes are the Host ("h"s) : nnnnnnnn.nnnnnnnn.hhhhhhhh.hhhhhhhh
Class C - the fist three bytes codes the Network ("n"s) and the last bytes are the Host ("h"s) : nnnnnnnn.nnnnnnnn.nnnnnnnn.hhhhhhhh
.
Here is a summary of number of networks and number of hosts per Class:
Class A - 2^7-2 = 126 networks - 2^24-2 = 16777214 hosts
Class B - 2^14-2 = 16382 networks - 2^16-2 = 65534 hosts
Class C - 2^21-2 = 2097150 networks - 2^8-2 = 254 hosts

What is a Subnet ?
.
Once you have been allocated a Network by the IANA (for example a the Class B Network: 150.10.0.0), you have 65534 IP addresses available... You wont have all these IPs on the physical sub-network, will you ? That's the main reason for creating subnets. Another big reason is security: subneting enables using security rules between subnets.
.
The Host part of the IP address is divided in two parts, the first one codes the Subnet and the second one codes the Host. The number of bits used for the Subnet is variable and depends on the number of hosts you need in this subnet (and vice versa).
.
For example, you can decide to have for a Class B: nnnnnnnn.nnnnnnnn.ssssssss.hhhhhhhh where "n"s represent the network, "s"s the Subnet and "h"s the Host.
In this example you have 8 bits for the Subnet and 8 bits for the Host.
But you can also choose to to use: nnnnnnnn.nnnnnnnn.sssssshh.hhhhhhhh, 6 bits for the Subnet and 10 for the Host.
.
It is not recommended to use subnets with all "s"s to "0" or all "s"s to 1 but it is tolerated. However in a production environment, it is strongly unrecommended since "old" standards prohibit these subnets.
.
In the Host part, all "h" bits to "0" is the "Subnet Address" (also called "Network address" or "Network Number") and all to "1" is the Broadcast Address.

What is a Subnet Mask ?
.
The Subnet Mask is deduced from the subnet structure (See "What is a subnet ?").
All bits corresponding to the Network and Subnet are "1"s and all bits corresponding to the Host are "0"s.
.
For example:
To the Class B IP: nnnnnnnn.nnnnnnnn.sssssshh.hhhhhhhh
Corresponds the subnet mask: 11111111.11111111.11111100.00000000
(where "n"s represent the Network, "s"s the Subnet and "h"s the Host)
.
This Subnet Mask in dotted decimal notation is 255.255.252.0. The "number of bits set to 1" notation is also commonly used. In this example, there are 22 "1"s so we write "/22".

How to calculate the Subnet Mask ?
.
This is the aim of the Mask Calculator. It helps you find out how big you need the Subnet part to be and how big you need the Host part to be, depending on your needs.
.
The Subnet Mask and Class determines how many subnets and hosts you get (i.e. how you subnet you network).
.
The number of hosts is: 2^(number of host bits)-2
All host bits to "0" is the "Subnet Address" (also called "Network address" or "Network") and all to "1" is the Broadcast Address.
.
The number of subnets is: 2^(number of subnet bits) if we tolerate all "0"s and all "1"s
or: 2^(number of subnet bits)-2 if not
.
For example:
A Class B IP subneted has follows: nnnnnnnn.nnnnnnnn.sssssshh.hhhhhhhh
(where "n"s represent the Network, "s"s the Subnet and "h"s the Host)
Has got: 2^6=64 subnets and 2^10-2=1022 hosts
For each subnet:
The Subnet Address is nnnnnnnn.nnnnnnnn.ssssss00.00000000
And the Broadcast Address is nnnnnnnn.nnnnnnnn.ssssss11.11111111

How to calculate the Subnet Address ?
.
This is the aim of the Subnet Calculator. It calculates the Subnet Address, Broacast Address and the number of subnets and hosts per subnet.
.
The Subnet Address is obtained doing a binary AND between the IP address and the Subnet Mask (see "Binary Operators").
.
For example, if we have the IP 150.10.10.10 with a mask 255.255.252.0 (also written 150.10.10.10/22)
.
The IP in binary is: 10010110.00001010.00001010.00001010 150.10.10.10
The mask is: 11111111.11111111.11111100.00000000 255.255.252.0
Binary AND -------------------------------------- -------------
Subnet Address: 10010110.00001010.00001000.00000000 150.10.8.0

How to calculate the Broadcast Address ?
.
The Broadcast Address is obtained doing a binary OR between the Subnet Address and the inverted Subnet Mask (see "Binary Operators").
.
For example, if we have the IP 150.10.10.10 with a mask 255.255.252.0 (also written 150.10.10.10/22), the Subnet Address is 150.10.8.0 (see "How to calculate the Subnet Address ?").
.
Subnet Address : 10010110.00001010.00001000.00000000 150.10.8.0
The inverted mask: 00000000.00000000.00000011.11111111 0.0.3.255
Binary OR -------------------------------------- ------------
Broadcast Address: 10010110.00001010.00001011.11111111 150.10.11.255

What is binary ?
.
Binary is a base 2 number system... What does it means ?
It means that after 1, there is no 2 but 10 instead. Strange ?
Each binary digit is called "bit" and has a value of either "0" or "1".
Note that a chain of 8 bits is called a "byte" (or "octet").
.

This is the way to count up to 10 in binary:0 0
1 1
10 2
11 3
100 4
101 5
110 6
111 7
1000 8
1001 9
1010 10

Could you guess what is 11 in binary ?
.
Binary is the fundamentals of computing. Computers understand only "0"s and "1"s (which correspond to "no electrical signal" or "electrical signal"). But with that, they can do quite a lot

How to convert binary to decimal ?

To convert binary to decimal, we start from the right to the left of the binary chain and at each bit, we associate the value 2^(character number), the first bit being bit number 0.
Note:
2^0 = 1
2^1 = 2
2^2 = 2 x 2 (2 times)
2^3 = 2 x 2 x 2 (3 times)
.
For example: 1 1 0 0 1 0 1 1
<------------------------------------------
power of 2: 2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0
Value: 128 64 32 16 8 4 2 1
.
The decimal value is the sum of the binary charaters times the powers of 2.
.
For the example above:
Result = 1 x 1 + 1 x 2 + 0 x 4 + 1 x 8 + 0 x 16 + 0 x 32 + 1 x 64 + 1 x 128 = 203
.
The minimum value is all bits set to "0" which is "0" in decimal and the maximum value for 8 bits (a byte) is all bits set to "1" which is "255" in decimal.
.
For a chain of n bits, the maximum is (2^n)-1. For example for a word (2 bytes), the maximum is 2^16-1=65535

How to convert decimal to binary ?
.
To convert decimal to binary, we first find the nearest power of 2 to the decimal number, then we substract it to the number. We do the same with the result of the substraction and so on until the result is 0.
.
For example, the decimal number 203 is in binary:

203-128=75 (128=2^7)
75-64=11 (64=2^6)
11-8=3 (8=2^3)
3-2=1 (2=2^1)
1-1=0 (1=2^0)

We put a "1" for each power of 2 "used during the calcul process" and "0" for each "not used" as follows:
Value: 128 64 32 16 8 4 2 1
power of 2: 2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0
Binary number: 1 1 0 0 1 0 1 1

What is hexadecimal ?
.
Hexadecimal is a base 16 number system. Hexadecimal numbers correspond to decimal numbers as follows:
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
A 10
B 11
C 12
D 13
E 14
F 15
10 16
11 17
...

.

Hexadecimal is very common in computing because 2 hexadecimal digits code 8 bits (1 byte) and conversions between hexadecimal and binary are very easy (see "How to convert hexadecimal to binary ?" and "How to convert binary to hexadecimal ?").

In networks, for example, we use the hexadecimal notation for Media Access Control (MAC) addresses coded on 48 bits, i.e. 12 hexadecimal digits.

How to convert binary to hexadecimal ?
.
To convert binary to hexadecimal, at each 4-bits portion of the binary number, we associate the hexadecimal number using the following table:
0000 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
1000 8
1001 9
1010 A
1011 B
1100 C
1101 D
1110 E
1111 F

.
For example, 1111110010110001=FCB1:
1111 1100 1011 0001
F C B 1

How to convert hexadecimal to binary ?
.
To convert hexadecimal to binary, at each hexadecimal digit we associate the 4 bits binary number using the following table:
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
A 1010
B 1011
C 1100
D 1101
E 1110
F 1111

.
For example, FCB1=1111110010110001:
F C B 1
1111 1100 1011 0001

Binary operators
.
The binary AND is defined as follows:

0 AND 0 = 0
1 AND 0 = 0
0 AND 1 = 0
1 AND 1 = 1

"1 if both bits to , 0 otherwise"


The binary OR is defined as follows:
0 OR 0 = 0
1 OR 0 = 1
0 OR 1 = 1
1 OR 1 = 1

"1 if one bit is 1 or both are 1, 0 otherwise"


The binary XOR (eXclusive OR) is defined as follows:
0 XOR 0 = 0
1 XOR 0 = 1
0 XOR 1 = 1
1 XOR 1 = 0

"1 if one bit is 1 or the other to 1 but not NOT both to 1"


The binary NOT (inversion) is defined as follows:
NOT 0 = 1
NOT 1 = 0

"1 if 0, 0 if 1" : invert the bit

(ShunSoft)