Thursday, April 26, 2007

Bit by Bit...

What is the smartest way of counting the number of bits in a byte? (psuedocode please)

4 Comments:

Anonymous Anonymous said...

English please! What's a pseudocode?

Fri May 11, 01:50:00 AM 2007  
Blogger littlecow said...

A psuedocode is a program written without the syntax of an actual computer language. Essentially, the question asks for an algorithm.

Fri May 11, 05:25:00 AM 2007  
Blogger littlecow said...

A little bit of background on this question might help: Counting bits is often an issue while programming a microcontroller. Most of the micros these days have plenty of code memory to null this issue. However, it is still an interesting and useful challenge to avoid the "for...do" loop that is typically associated with counting (thereby making the code faster and smaller).

Indeed, this counting can be achieved with some elementary bitwise boolean, multiplication and bit-shifting operations, which is really what makes this problem fabulous!

Fri May 11, 05:31:00 AM 2007  
Blogger KG said...

Pseudocode:
1. Start
2. Return 8
3. Stop

:-D

I assume you want to count the number of set bits. Assuming you want to do this many times (otherwise the speed isn't that important an issue), this allows for a slower setup step done only once followed by a fast step which is done every time you want to count set bits in a byte.

The smartest way I can think of is to have an array of 128 integers (bytes or words, whatever is faster). I define the array[value] to be the number of set bits in the byte of that value. Then each set bit count is just a retrieval from the array.

-Grenade (forgot password)

Wed Jun 06, 11:39:00 AM 2007  

Post a Comment

<< Home