Finding NULL byte in a long
Tony Lezard
tony at mantis.co.uk
Fri Dec 14 22:34:41 AEST 1990
pmk at 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 at 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.
More information about the Comp.lang.c
mailing list