# IC to count number of inputs that are on?

I have 8 wires, and I want to count how many of these are in a high state and convert this to a 3-bit binary signal. If the count of inputs that are high is greater than 4, I don't care what exact amount of inputs are on beyond that point, I just need to know whether more than 4 are high. So I only need 3 bits of output, if that makes sense.

i.e. if 1 wire is on, output 001, if 2 wires on on, output 010, etc. and if 7 or 8 wires are on output 111.

Is there an IC that can help me do this easily?

• You statements are contradictory. First, you say that you don't care exactly what number are high if 4 or more are high, then you say the output should be 111 of 7 or 8 are high. Please provide a chart of inputs high vs. output bits. Commented Mar 27, 2014 at 19:48
• Sorry for the confusion. Well, since I need to know whether the output is greater than 4, I need 3 bits of output anyways. So really anything signaling greater than 4 works, I figured the most likely way to do it would be just count normally until 8 is reached, and when that happens just count that as a 7.
– Gus
Commented Mar 27, 2014 at 19:56
• The answer you accepted is wrong. This is a good example of why you should wait a little while before accepting something. You want to give various people a chance to chime in, and for the peer review process to tell you which answers are right and which ones are wrong. Commented Mar 27, 2014 at 20:42

This is a simple job for a microcontroller. Spehro correctly points out that it can be accomplished with a simple lookup. If you can tolerate lower speed then counting the bits in a loop will do it too, but will take less code space.

Another way is to do this in analog. Put a 100 kΩ resistor in series with each digital output, then feed that into a comparator. If 4 out of 8 are high, then you get 0.50 of the supply voltage. If 3 out of 8 are high, then you get 0.38 of the supply voltage. Set the comparator threshold to 0.44 of the supply and it will distinguish between 4 or more, and 3 or less.

• If there's a spare ADC input that would work too. Commented Mar 27, 2014 at 21:12

A microcontroller could do it very easily. Since there are only 256 possible combinations, a simple lookup table would be the fastest way. (I did the below outputting 8 for all high for consistency, you can change the last entry to 7 if you want).

The code could look something like this:-

unsigned char lut[256] = {0x0, 0x1, 0x1, 0x2, 0x1, 0x2, 0x2, 0x3, 0x1, 0x2, 0x2, 0x3, 0x2, 0x3, 0x3, 0x4, 0x1, 0x2, 0x2, 0x3, 0x2, 0x3, 0x3, 0x4, 0x2, 0x3, 0x3, 0x4, 0x3, 0x4, 0x4, 0x5, 0x1, 0x2, 0x2, 0x3,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, 0x5, 0x5, 0x6, 0x5, 0x6, 0x6, 0x7, 0x5, 0x6, 0x6, 0x7, 0x6, 0x7, 0x7, 0x8};

<some initialization stuff>

for(;;) out_port= lut[in_port];  // do this forever


I don't know of any logic chip that has this exact functionality. Priority encoders do something a bit different.

If you needed really fast response time, you could program a nonvolatile memory with the lookup table, or use programmable logic.

Shouldn't a 4-bit full adder do the trick?