Determine the network and host ID portion of an IP address

98,568

Solution 1

You're over-complicating things.

IPv4 addresses (and subnet masks) are merely displayed in dot-decimal notation simply as a means of making them more readable to humans. Within the computer, they are simply 4 bytes of contiguous memory (often stored, for example, within a long int):

Stored in computer:    11000000 10101000 00100001 00010110
Displayed for human:        192.     168.      33.      22

Stored in computer:    11111111 11111111 11100000 00000000
Displayed for human:        255.     255.     224.       0

The 1s in the mask indicate bits that identify the network number, thus one merely need use a bitwise AND operation to extract the network number:

address   11000000 10101000 00100001 00010110    192.168.33.22
mask      11111111 11111111 11100000 00000000    255.255.224.0
(AND)     -----------------------------------    -------------
network   11000000 10101000 00100000 00000000    192.168.32.0

Since the introduction of CIDR (prior to which the address's class indicated the network/host boundary), hosts usually only know the mask of their own network and are therefore unable to divide arbitrary addresses (e.g. that of a datagram's destination) into network and host numbers.

So what's the point? Well, a source host can still take the bitwise AND of the destination's address and its (the source's) own network mask. Whilst the result of that operation will not necessarily produce a meaningful network number, it will match the source's network number if and only if they are on the same network:

  • if they match, the destination should be reachable at the link layer (e.g. by looking up its MAC address, perhaps via broadcasting an ARP request, and then encapsulating the datagram in a frame that is addressed to that MAC);

  • if they differ, the source must send the datagram to a router that is on its own network (using the above process to reach that router); the router will see that the frame is addressed to it, but that the datagram is not, and should then forward the datagram (encapsulated in a different frame) towards the destination. Many hosts only know of one router, their "default gateway", although other configurations are possible.

Those address bits that don't identify the source's network number, evidently indicated by 0s in its network mask, can be considered to form its host number—although it's really neither meaningful nor useful to extract it in the same way as was done above: even when communicating with a host on one's own network, its full address is used for identification, never the host number alone.

That said, as a purely academic exercise it is of course possible to perform a bitwise AND with the complement of the mask:

address   11000000 10101000 00100001 00010110    192.168.33.22
~mask     00000000 00000000 00011111 11111111    0.0.31.255
(AND)     -----------------------------------    -------------
host      00000000 00000000 00000001 00010110    0.0.1.22

So, to address your questions:

  1. Is the host ID the public part? Is the network ID the private part for locating the computer within the local network?

    The entire address is "public"; there are no "private" parts. Lookup protocols like ARP (which uses the full address) are used to locate computers within the local network.

  2. If the subnet mask is a value smaller than 255 the corresponding octet in the IP address must be broken down into binary to determine which part of the number is the host ID and which portion is the network ID. Is the result binary number always split in two?

    Nothing is "split in two". It only appears that way because dot-decimal notation was intended to make IPv4 addresses more readable to humans (albeit that decision was taken prior to the invention of CIDR, when network numbers were always aligned to byte boundaries and thus never caused the apparent "split" of a decimal number).

Solution 2

  • Is the host ID the public part? Is the network ID the private part for locating the computer within the local network?

The host and network portions of an ip address have nothing to do with public and private.

  • If the subnet mask is a value smaller than 255 the corresponding octet in the IP address must be broken down into binary to determine which part of the number is the host ID and which portion is the network ID. Is the result binary number always split in two? ...a subnet mask of 255.255.224.0 means that the octet holding 33 be broken down as follows: 0010|0001...

Your example is wrong. Specifically, you assume that 224 has four consecutive binary bits in it when you spit the 33 octet as 0010|0001 (where | is the division between network and host)...

The octet in the subnet mask containing 224 has three consecutive binary 1s in it: 11100000. Therefore the "network portion" of the whole IP address is: 192.168.32.0. The "host portion" of the ip address is 0.0.1.22. Using your notation, the third octet of ip 192.168.33.22 (mask 255.255.224.0) is: 001|00001.

To get the network portion of an IP address, you must perform a binary AND of the ip address and its netmask. The host portion is a binary AND of the inverted netmask (bits flipped between 0 and 1).

EDIT

Let's make another example to address your comment:

IP Address 192.168.255.22, NetMask 255.255.224.0

The network portion of this address is 192.168.224.0 and the host portion of the address is 0.0.31.22. I intentionally chose the numbers in the example to make the math as obvious as possible. Please convert 224 and 31 to binary, it should make things clear. If not, please reference the wikipedia article on subnetting

Solution 3

Host address portion and network address portion can be easily identified.
Use this trick.
 Class A: N.H.H.H
 Class B: N.N.H.H
 Class C: N.N.N.H
(N= network  H=Host)
Class A network range: 1-127
Class B network range: 128-191
Class C network range: 192-223

Reference: https://www.youtube.com/watch?v=ddodZeXUS0w

Share:
98,568
fwc
Author by

fwc

Stuff and junk

Updated on April 18, 2020

Comments

  • fwc
    fwc about 4 years

    I need to work out the algorithm regarding how you calculate the network and host portion of an IP address.

    1. Is the host ID the public part? Is the network ID the private part for locating the computer within the local network?

    2. If the subnet mask is a value smaller than 255 the corresponding octet in the IP address must be broken down into binary to determine which part of the number is the host ID and which portion is the network ID. Is the result binary number always split in two?

      (e.g. An IP address of 192.168.33.22 with a subnet mask of 255.255.224.0 means that the octet holding 33 be broken down as follows: 0010|0001 indicating that 0010 is the network ID portion and 0001 is the host ID portion?)

    Thank you in advance for any help.

  • fwc
    fwc over 12 years
    Hi Mike, I am not sure I follow the last portion about the network and host ID portion. Are you saying that because the binary version of 224 is 11100000, the network portion with be the part corresponding with the three 1's? and the host ID portion is the section associated with the 0's? Thanks again for your explanation.
  • fwc
    fwc over 12 years
    yes, that answers my question. it seems the algorithm uses 1s part of the binary from the subnet mask. this would mean that the same IP address from my example with the third octet as 31 would have a host ID of 0.0.31.22. Correct me if I am wrong here.
  • Mike Pennington
    Mike Pennington over 12 years
    @fwc, you're wrong. Read what I said in my answer about 192.168.33.22 and mask 255.255.224.0
  • fwc
    fwc over 12 years
    @MikePennington- I would calculate it as follows: subnet mask for the third octet is still 11100000, so the binary for 33 is 00011111; therefore the it would split as follows 000|11111 where the line is the divider between the network and host ID, or am i still missing something?
  • Mike Pennington
    Mike Pennington over 12 years
    @fwc, please check your binary math in the python interpreter. >>> int('00011111', 2). That comes out to 31, not 33
  • eggyal
    eggyal over 2 years
    Classful addressing was replaced by CIDR in 1993.