Jump to content

Recommended Posts

Posted
Hi all - If I have a given word (or a double word), is there a way for me to tell how many bits within that word are set at '1'? For example, MW323 = 0010000111010011, I would like to know that there are 7 bits that are a '1'. I don't care what value (hex, binary, decimal, etc) they may represent, just the number (quantity) of bits set at '1'. Thanks for any help!

Posted
there are many ways to do what you ask. if you have to do it on your own, probably the simplest way is to make a loop and just count the set bits, something like: 1. clear counter 2. if the bit is high increment counter 3. if the last bit, you are done 4. shift the dword or what ever the bit field you work on (one place only) 5. jump to step 2 there are also methods to count bits in parallel using algorythms, sometimes significantly improving performance: http://www.google.com/search?hl=en&as_...el+bit+counting

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...