Path: utzoo!attcan!telly!lethe!torsqnt!news-server.csri.toronto.edu!rutgers!mit-eddie!snorkelwacker.mit.edu!apple!usc!cs.utexas.edu!uunet!mcsun!ukc!slxsys!ibmpcug!mantis!tony From: tony@mantis.co.uk (Tony Lezard) Newsgroups: comp.lang.c Subject: Re: Finding NULL byte in a long Message-ID: Date: 14 Dec 90 11:34:41 GMT References: <1990Dec13.153552.1862@craycos.com> Organization: Mantis Consultants, Cambridge. UK. Lines: 23 pmk@craycos.com (Peter Klausler) writes: >Finding the *position* of the null byte is a little trickier. It helps a lot >if your machine has a "leading zero count"/"find first set bit" instruction >and your compiler lets you use it. Assuming you're using 2's compliment arithmetic, there's always a "find first set bit" instruction. For any x, the expression x & -x yields all zeros except for a 1 in the position of the first set bit from the right. For example, if x = ...000010011001000 -x = ...111101100111000 => x & -x = ...000000000001000 ^ `-- Voila! I don't know how well known this is. There are even occasions where it is actually quite useful! ========================================================================== Tony Lezard. E-mail: tony@mantis.UUCP, Snail-mail: Mantis Consultants, Unit 56, St. John's Innovation Centre, Cambridge, CB4 4WS, United Kingdom. Most appropriate anagram of name: Lazy Rodent.