home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!uunet!not-for-mail
- From: avg@rodan.UU.NET (Vadim Antonov)
- Newsgroups: comp.programming
- Subject: Re: finding 1st one in integer
- Date: 21 Jul 1992 16:38:28 -0400
- Organization: Berkeley Software Design
- Lines: 27
- Message-ID: <14hsk4INNke@rodan.UU.NET>
- References: <Brqu3F.1J4@undergrad.math.waterloo.edu> <1992Jul21.173805.12045@bcrka451.bnr.ca>
- NNTP-Posting-Host: rodan.uu.net
-
- 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
-