Re: Interesting Quest - Any optimized way to find if consective one's exist in a word!

From: Norbert Juffa (juffa_at_earthlink.net)
Date: 02/08/05


Date: Tue, 08 Feb 2005 16:41:29 GMT


"shafique" <news4shafique@lycos.co.uk> wrote in message news:1107869766.357206.39200@l41g2000cwc.googlegroups.com...
[...]
> Can anybody have idea as how to find it there exist consective one's in
> a word/dword using bitwise operatiors in C language.
>
> Obviously this can be done by shifting and looping one by one; but
> there must be some faster way of doing it.
>
> The thing I want is to define some masks and macros and find if there
> are consective ones on a word/dword. e.g. 0000b has no consective
> ones.... 00011000b has two consective ones, 0001101... has no
> consective ones 000001000 has one consective one! So the requiement is
> to find out if there exists any 0 sandwitched between two ones.
[...]

Please note that your question is off-topic in comp.lang.c. This group
is for discussing ANSI C, not algorithms. A more appropriate newsgroup
for your question seems to be comp.programming.

It is a bit unclear exactly what the algorithm you are looking for is
supposed to do. You may want to look up section 6-2 "Find First String
of 1-Bits of a Given Length" in the book "Hacker's Delight" by Henry S.
Warren, Jr (Addison-Wesley, 2003), as that sounds close to what you want
to do.

-- Norbert



Relevant Pages