Click here to Skip to main content
15,890,825 members
Home / Discussions / Algorithms
   

Algorithms

 
QuestionMy sorting algorithm Pin
AksharRoop6-Jul-10 4:53
AksharRoop6-Jul-10 4:53 
AnswerRe: My sorting algorithm Pin
Luc Pattyn6-Jul-10 5:20
sitebuilderLuc Pattyn6-Jul-10 5:20 
GeneralRe: My sorting algorithm Pin
AksharRoop6-Jul-10 5:24
AksharRoop6-Jul-10 5:24 
GeneralRe: My sorting algorithm Pin
RugbyLeague6-Jul-10 22:41
RugbyLeague6-Jul-10 22:41 
GeneralRe: My sorting algorithm Pin
molesworth7-Jul-10 1:33
molesworth7-Jul-10 1:33 
GeneralRe: My sorting algorithm Pin
RugbyLeague7-Jul-10 2:37
RugbyLeague7-Jul-10 2:37 
QuestionCounting set bits in bitmap Pin
Tadeusz Westawic24-Jun-10 4:51
Tadeusz Westawic24-Jun-10 4:51 
AnswerRe: Counting set bits in bitmap [modified] Pin
harold aptroot24-Jun-10 5:14
harold aptroot24-Jun-10 5:14 
What kind of "bitmap" do you mean?
The fastest way (excluding straight table lookup of course, but that only works well if the table is in cache) to count bits in an integer (without using the popcnt instruction, which is not commonly supported) is this: http://stackoverflow.com/posts/1511920/revisions[^]
It's based on this: http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel[^]

edit: ok to clear the mess I made here up a bit..
There are many ways, including:
- popcnt: not supported by enough CPU's yet
- table lookup: only works well if you can keep the table in cache until you don't have to count any bits anymore, a cache miss is many time more terrible even than using the "standard" way (so if you have to count a lots of bits in a tight loop, go for it)
- count the bits one by one[^], works well if you expect few bits to be set (or reset - just take the complement and subtract the count from the length)
- count the bits in parallel (see links in the first part of my post) - it has no bad case, making it a safe choice. It simply uses a fixed number of steps, without needing big tables.

modified on Thursday, June 24, 2010 11:22 AM

GeneralRe: Counting set bits in bitmap [modified] Pin
Tadeusz Westawic24-Jun-10 6:07
Tadeusz Westawic24-Jun-10 6:07 
GeneralRe: Counting set bits in bitmap Pin
harold aptroot24-Jun-10 6:19
harold aptroot24-Jun-10 6:19 
GeneralRe: Counting set bits in bitmap Pin
harold aptroot24-Jun-10 7:01
harold aptroot24-Jun-10 7:01 
GeneralRe: Counting set bits in bitmap Pin
Tadeusz Westawic24-Jun-10 7:46
Tadeusz Westawic24-Jun-10 7:46 
GeneralRe: Counting set bits in bitmap Pin
Luc Pattyn24-Jun-10 8:06
sitebuilderLuc Pattyn24-Jun-10 8:06 
GeneralRe: Counting set bits in bitmap Pin
Tadeusz Westawic24-Jun-10 10:27
Tadeusz Westawic24-Jun-10 10:27 
GeneralRe: Counting set bits in bitmap [modified] Pin
Luc Pattyn24-Jun-10 11:00
sitebuilderLuc Pattyn24-Jun-10 11:00 
GeneralRe: Counting set bits in bitmap Pin
harold aptroot24-Jun-10 8:07
harold aptroot24-Jun-10 8:07 
GeneralRe: swimming adder Pin
Luc Pattyn24-Jun-10 8:32
sitebuilderLuc Pattyn24-Jun-10 8:32 
GeneralRe: Counting set bits in bitmap [modified] Pin
Tadeusz Westawic24-Jun-10 9:04
Tadeusz Westawic24-Jun-10 9:04 
GeneralRe: Counting set bits in bitmap Pin
harold aptroot24-Jun-10 11:15
harold aptroot24-Jun-10 11:15 
GeneralRe: Counting set bits in bitmap Pin
Tadeusz Westawic24-Jun-10 11:47
Tadeusz Westawic24-Jun-10 11:47 
GeneralRe: Counting set bits in bitmap Pin
Luc Pattyn24-Jun-10 12:26
sitebuilderLuc Pattyn24-Jun-10 12:26 
GeneralRe: Counting set bits in bitmap Pin
Tadeusz Westawic24-Jun-10 14:12
Tadeusz Westawic24-Jun-10 14:12 
GeneralRe: Counting set bits in bitmap Pin
Member 419459326-Jun-10 17:28
Member 419459326-Jun-10 17:28 
GeneralRe: Counting set bits in bitmap Pin
harold aptroot27-Jun-10 2:28
harold aptroot27-Jun-10 2:28 
GeneralRe: Counting set bits in bitmap Pin
Member 419459327-Jun-10 5:49
Member 419459327-Jun-10 5:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.