Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Rainer Weikusat Newsgroups: comp.programming,comp.unix.programmer Subject: Re: hash function over IP address Date: Thu, 05 Apr 2012 12:15:40 +0100 Lines: 22 Message-ID: <87fwcio2eb.fsf@sapphire.mobileactivedefense.com> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: individual.net iAdXJl3ITsg306vcTVSwNAPbN95sqM0isIMytj8rR5lCfw0/E= Cancel-Lock: sha1:tYInuBRewJIy/KoqtyyTwVLE5D8= sha1:bWOsVyHb1RK9H2gwf3V6dBJQKwU= User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.2 (gnu/linux) Xref: csiph.com comp.programming:1422 comp.unix.programmer:2363 "Mark" writes: > unsigned long hash_ipaddr(struct in_addr *addr) > { > unsigned long res; > > if (addr == NULL) > return 0UL; > > res = (((addr->s_addr >> 24) & 0xff) * 256) + \ > (((addr->s_addr >> 16) & 0xff) * 256) + \ > (((addr->s_addr >> 8) & 0xff)* 256) + \ > (addr->s_addr & 0xff); > > return res; > } Unrelated question (Barry Margolin also noticed that but I'd like to try expressing it somewhat clearer): This function adds the three highest octets together in the high octet of a 16-bit integer and then adds the lowest octet of the original address to the lowest octet of this 16-bit integer whose value was zero before this addition. This seems rather odd. Is this intentional?