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

Algorithms

 
AnswerRe: Counting set bits in bitmap [modified] Pin
harold aptroot24-Jun-10 5:14
harold aptroot24-Jun-10 5:14 
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 
OK. It sounds like your bits are sparse then, so I'd go for a sequential count, probably like this (C#):

public int CountBits(uint[] data) {
    int count=0;
    foreach (uint dd in data) {
        uint d=dd;
        while(d!=0) {
            count++;
            d&=d-1;
        }
    }
    return count;
}


or a table lookup, which works fine for bytes and maybe shorts (for 32-bit data you'd have to split in two or four bitgroups to keep the table small and cachable):

int[] bitCountInByte=new int[256];  // needs initialization

public int CountBits(byte[] data) {
    int count=0;
    foreach (byte d in data) {
        if (d!=0) count+=bitCountInByte[d];
    }
    return count;
}


Smile | :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.


modified on Thursday, June 24, 2010 5:30 PM

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 
AnswerRe: Counting set bits in bitmap Pin
Luc Pattyn24-Jun-10 5:20
sitebuilderLuc Pattyn24-Jun-10 5:20 
Questionhex calculator Pin
Tadeusz Westawic23-Jun-10 2:50
Tadeusz Westawic23-Jun-10 2:50 
AnswerRe: hex calculator Pin
Richard MacCutchan23-Jun-10 2:58
mveRichard MacCutchan23-Jun-10 2:58 
GeneralRe: hex calculator Pin
Tadeusz Westawic23-Jun-10 16:40
Tadeusz Westawic23-Jun-10 16:40 
GeneralRe: hex calculator Pin
Richard MacCutchan23-Jun-10 21:42
mveRichard MacCutchan23-Jun-10 21:42 
GeneralRe: hex calculator Pin
wbgxx29-Jun-10 7:12
wbgxx29-Jun-10 7:12 
AnswerRe: hex calculator Pin
Luc Pattyn23-Jun-10 3:54
sitebuilderLuc Pattyn23-Jun-10 3:54 

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.