Mega Code Archive

 
Categories / Delphi / Algorithm Math
 

IP address calculations 01

Title: IP address calculations -01 Question: How to calculate the number of bits used by an IP mask / address Answer: (* this function takes a numeric IP address and returns the number of bits set to 1. Useful to calculate the number of possible hosts or subnets in a network. *) function calcMaskBits(aIPMask:longWord):byte; var i,bits : Byte; jShift : longWord; Begin jShift:=1; if (aIPMask and jShift)0 then bits:=1 else bits:=0; for i := 0 to 31 do begin if (aIPMask and (jShift shl i))0 then inc(bits); end; result:=bits; End;