'How do I calculate IP-header length?

I want to calculate IP-header length with following statement:

Header Length is a four-bit field that tells, as the name implies, the length of the IP header in 32-bit words

Now I'm getting difficulties in calculating IP header length (minimum and maximum), with four-bit field.



Solution 1:[1]

minimum value of header length is 20 Bytes but we don't have sufficient bits to represent 20 so we use scaling technique, i.e. 0101 (5) will represent 4 X 5 = 20 Bytes, here scaling factor is 4.

maximum value possible with 4 bits is 15.

So maximum header length possible is 4 X 15 = 60 Bytes.

Header Length |  Header Length Field

         20 -----> 5
         24 -----> 6
         28 -----> 7
            .
            .
            .
         60 -----> 15

if header length is 22 Bytes then we use padding to make it a multiple of 4 i.e. 24 Bytes

Solution 2:[2]

@Amit>Value in the HL filed is = the number of 4 bytes in the total IP header length.Means if the header length field is say 40,then calculate how many 4 bytes in 40?its 40/4= 10.So value in the HL field is 10

MINIMUM HL value

Minimum HL is 20 byte .ie no of 4bytes in 20 = 20/4=5.So minimum value in HL field is 5.

MAXIMUM HL value

HL is a 4 bit field.so the maximum vale that can be accomadated in that field is 15(1111) or you can calculate using the formulae 2^4-1=15.So max no: of 4 bytes can be 15.Hence the Max header length=15*4=60bytes.

Hope now things are clear.

Solution 3:[3]

The Internet Header Length (IHL) field is the number of 32-bit words(=4 bytes) in the IPv4 header, including any options. Because this is also a 4-bit field, the IPv4 header is limited to a maximum of fifteen 32-bit words(=60 bytes)

So if the value of IHL is 0101(5), then the IP-header's length is 5*4(bytes) = 20(bytes)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vikrant Singh
Solution 2 Poorvi
Solution 3