#### Archived

This topic is now archived and is closed to further replies.

## Recommended Posts

Cyberdrek    100
Ok, here's something that's been on my bugging me since last night, I have a function that receives flags(ex: draw( x, y, BLAH | BLAH2 ); ) to do is get the number of bits that are set in the flag. Here's an example of how the flags are defined. const int BLAH = 128; const int BLAH2 = 64; const int BLAH3 = 32; const int BLAH4 = 16; const int BLAH5 = 8; const int BLAH6 = 4; const int BLAH7 = 2; const int BLAH8 = 1; So lets say I pass BLAH | BLAH2, I know that I have 2 bits set but lets say that I don't know the number that's going to be passed and I want to count how many bits are set, is there a way that I can do it??? "And that's the bottom line cause I said so!" ** I WANT TO BE THE MODERATOR FOR THE LINUX FORUM ** Cyberdrek Resist Windows XP's Invasive Production Activation Technology! "gitty up" -- Kramer /(bb|[^b]{2})/ that is the Question -- ThinkGeek.com Hash Bang Slash bin Slash Bash -- #!/bin/bash Edited by - cyberdrek on December 17, 2001 9:13:03 PM

##### Share on other sites
krez    443
of course there is a way you can do that (probably there are several). unfortunately i don''t know any of them.

um, you could have a bunch of "if" statements, and increment some counter variable every time one of them is true (if(lFlags & BLAH4) numFlags++; )... that''s a lousy way to do it though...
er... your elegant solution lies in bitshifts i think... hmm... i''ll be back...

--- krez (krezisback@aol.com)

##### Share on other sites
scaught    122
There''re three ways to do it - the math way and two brute force ways:

Math way (from another post I made):
  int countBits( int value ){ if( !value ) { return 0; } return( (int)( log10( value ) / log10( 2 ) ) + 1 );}

Brute Force 1: if() statements as krez mentioned

Brute Force 2:
  unsigned int countBits( unsigned int value ){ int count = 31; if( !value ) { return 0; } while( ( value & ( 1 << count ) ) { --count; } return( count + 1 );}

(standard disclaimers apply - I didn''t test it but it looks like it should work...)

btw - I think I''d go with BF2 if I had to pick one.

-scott

##### Share on other sites
scaught    122
heh...nevermind, those count the max bits. (don''t blame me, I have the flu)

here:

  int countBits( int value ){ int i, count = 0; for( i=0; i<32; ++i ) { if( value & ( 1 << i ) ) { ++count; } } return count;}

Sorry ''bout that...

-scott

##### Share on other sites
GoofProg    127
I would use powers of two comparison until you get the total number of bits.

##### Share on other sites
Cyberdrek    100
quote:
Original post by scaught
heh...nevermind, those count the max bits. (don''t blame me, I have the flu)

here:

  int countBits( int value ){ int i, count = 0; for( i=0; i<32; ++i ) { if( value & ( 1 << i ) ) { ++count; } } return count;}

Sorry ''bout that...

-scott

No problems, I''m just glad somebody could help me out, I''ve been at it for 2 days now...

Thanks again..

"And that''s the bottom line cause I said so!"

** I WANT TO BE THE MODERATOR FOR THE LINUX FORUM **

Cyberdrek

Resist Windows XP''s Invasive Production Activation Technology!

"gitty up" -- Kramer
/(bb|[^b]{2})/ that is the Question -- ThinkGeek.com
Hash Bang Slash bin Slash Bash -- #!/bin/bash

##### Share on other sites
Cyberdrek    100
quote:
Original post by GoofProg
I would use powers of two comparison until you get the total number of bits.

if I use power of 2s, I''ll have too many "if" statements which I''m trying to avoid...

"And that''s the bottom line cause I said so!"

** I WANT TO BE THE MODERATOR FOR THE LINUX FORUM **

Cyberdrek

Resist Windows XP''s Invasive Production Activation Technology!

"gitty up" -- Kramer
/(bb|[^b]{2})/ that is the Question -- ThinkGeek.com
Hash Bang Slash bin Slash Bash -- #!/bin/bash