home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.programming
- Path: sparky!uunet!s5!is1.is.morgan.com!is.morgan.com!berlin
- From: berlin@is.morgan.com (Alexander Berlin)
- Subject: Re: finding 1st one in integer
- Message-ID: <1992Jul21.165726@is.morgan.com>
- Sender: news@is.morgan.com
- Nntp-Posting-Host: chico
- Organization: Morgan Stanley - IS
- References: <Brqu3F.1J4@undergrad.math.waterloo.edu> <1992Jul21.173805.12045@bcrka451.bnr.ca> <14hsk4INNke@rodan.UU.NET>
- Date: Tue, 21 Jul 1992 20:57:26 GMT
- Lines: 34
-
- In article <14hsk4INNke@rodan.UU.NET>, avg@rodan.UU.NET (Vadim Antonov) writes:
- |> inline int highbitpos(register int i)
- |> {
- |> register int bitpos = 0;
- |>
- |> if( i & 0xffff0000 ) {
- |> i >>= 16;
- |> bitpos = 16;
- |> }
- |> if( i & 0xff00 ) {
- |> i >>= 8;
- |> bitpos += 8;
- |> }
- |> if( i & 0xf0 ) {
- |> i >>= 4;
- |> bitpos += 4;
- |> }
- |> bitpos += "\377\0\1\1\2\2\2\2\3\3\3\3\3\3\3\3"[i];
- |> return bitpos;
- |> }
- |>
- |> You can speed it up by sacrificing some data space and removing the
- |> last _if_. There are variants which do not requre additional register
- |> variable or addition at the end (the first makes the code bigger, the
- |> second requires replicating the bit tables). All those trade-offs are
- |> up to you :-)
- |>
- |> --vadim
-
- Vadim, you are assuming Big Endian, aren't you?
- The original article asked for PORTABLE code. ">>=" depends on byte order.
- ---
- Alex Berlin
- berlin@is.morgan.com
-