Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!tut.cis.ohio-state.edu!ucbvax!bloom-beacon!eru!hagbard!sunic!news.funet.fi!hydra!kreeta!wirzeniu From: wirzeniu@cs.Helsinki.FI (Lars Wirzenius) Newsgroups: comp.lang.c Subject: Re: Finding NULL byte in a long Message-ID: <9918@hydra.Helsinki.FI> Date: 11 Dec 90 11:40:26 GMT References: <1990Dec5.033206.10463@nimbus3.uucp> <198@nazgul.UUCP> Sender: news@cs.Helsinki.FI Organization: University of Helsinki, Department of Computer Science Lines: 19 In article <198@nazgul.UUCP> bright@nazgul.UUCP (Walter Bright) writes: >In article <1990Dec5.033206.10463@nimbus3.uucp> djs@nimbus3.UUCP (Doug) writes: >/I know this has been on the net before, since I thought I saved it, >/but can't find it now. Anyway, could someone tell me what the >/C expression is that tells you if a long has a NULL byte in it. >/This is without masking each byte and testing it for 0. It is very >/clever and non-obvious. Thanks. > >Hmmm, how about: > p = memchr(&x,0,sizeof(x)); How about (assuming sizeof(x) == 4, easily modified for other sizes): ((char *) &x)[0] == 0 || ((char *) &x)[1] == 0 || ((char *) &x)[2] == 0 || ((char *) &x)[3] == 0 Lars Wirzenius wirzeniu@cs.helsinki.fi wirzenius@cc.helsinki.fi