Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!wuarchive!uunet!ogicse!milton!uw-beaver!cornell!hilfingr From: hilfingr@rama.cs.cornell.edu (Paul N. Hilfinger) Newsgroups: comp.lang.c Subject: Re: Finding NULL byte in a long Message-ID: <49608@cornell.UUCP> Date: 11 Dec 90 21:01:51 GMT References: <1990Dec5.033206.10463@nimbus3.uucp> Sender: nobody@cornell.UUCP Reply-To: hilfingr@cs.cornell.edu (Paul N. Hilfinger) Organization: Cornell Univ. CS Dept, Ithaca NY Lines: 20 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. Normally, I'd reply by e-mail, but I can't believe that the formulae below are the best one can do, and I'd like to hear of better solutions myself. Assuming 32-bit, 2's complement longs, I believe the following expressions both have the property of being non-zero iff the long value x contains at least one 0 byte: (~x & 0x7f7f7f7f) + 0x01010101 & ~x & 0x80808080 ((x - 0x01010101) ^ x) & ~x & 0x80808080 Paul Hilfinger