Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to find longest bitrange of ones

Reply
Thread Tools

How to find longest bitrange of ones

 
 
longhorn
Guest
Posts: n/a
 
      05-07-2008
Hello,

I'm looking for a macro that will return the longest range of ones
in an unsigned integer, along with starting and ending position.

For example I have an unsigned int data that looks like:

bit pos: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 .... 00
data 0 1 1 1 1 0 1 1 0 1 0 1 1 0 ... 0

In this case I would get the longest range = 4, start=27, end=30.

Does anyone know a simple way to do this?

Thanks,
Kevin
 
Reply With Quote
 
 
 
 
Peter Nilsson
Guest
Posts: n/a
 
      05-07-2008
longhorn wrote:
> Hello,
>
> I'm looking for a macro that will return the longest range of ones
> in an unsigned integer, along with starting and ending position.
>
> For example I have an unsigned int data that looks like:
>
> bit pos: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 .... 00
> data 0 1 1 1 1 0 1 1 0 1 0 1 1 0 ... 0
>
> In this case I would get the longest range = 4, start=27, end=30.


Have you considered cases like: 01111011110...0

> Does anyone know a simple way to do this?


Hint:

What happens to a string of 1's in x if you do x &= x << 1?
What happens if you keep iterating?

--
Peter
 
Reply With Quote
 
 
 
 
Willem
Guest
Posts: n/a
 
      05-07-2008
longhorn wrote:
) Hello,
)
) I'm looking for a macro that will return the longest range of ones
) in an unsigned integer, along with starting and ending position.
)
) For example I have an unsigned int data that looks like:
)
) bit pos: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 .... 00
) data 0 1 1 1 1 0 1 1 0 1 0 1 1 0 ... 0
)
) In this case I would get the longest range = 4, start=27, end=30.
)
) Does anyone know a simple way to do this?

Do you want a clever bit hack, or a straightforward iteration ?


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Using Prime Time To Find All The Paths Of A Seq. Cir., Not Only the Critical Ones ilterisderici@hotmail.com VHDL 0 03-16-2006 09:46 PM
Longest Sites Name With Worlds Longest Stuffs... Vijay Murthy Computer Information 0 02-04-2006 09:57 AM
How do I find the length of the longest line in a file? John Howard Perl Misc 14 08-19-2005 02:49 PM
Hard drives--Are big ones more delicate than smaller ones? PowerPost2000 Computer Support 2 03-01-2005 10:30 PM



Advertisments