문제

Suppose I have a black box with 3 inputs (each input is 1 bit) and 2 bits output. The black box counts the amount of turned on input bits. Using only such black boxes,one needs to implement the counter of turned on bits in the input,which has 7 bits.The implementation should use the minimum possible amount of black boxes.

//This is a job interview question

도움이 되었습니까?

해결책

You're making a binary adder. Try this...
Two black boxes for input with one input remaining:

 7 6 5      4 3 2      1
 | | |      | | |      | 
-------    -------     |
|     |    |     |     |
| H L |    | H L |     |
-------    -------     |
  | |        | |       |

Take the two low outputs and the remaining input (1) and feed them to another black box:

            L L 1
            | | |
           -------
           |     |
           | C L |
           -------
             | |

The low output from this black box will be the low bit of the result. The high output is the carry bit. Feed this carry bit along with the high bits from the first two black boxes into the fourth black box:

 H H C   L
 | | |   |
-------  |
|     |  |
| H M |  |
-------  |
  | |    |

The result should be the number of "on" bits in the input expressed in binary by the High, Middle and Low bits.

다른 팁

Suppose that each BB outputs a 2-bit binary count 00, 01, 10, or 11, when 0, 1, 2, or 3 of its inputs are on. Also suppose that the desired ultimate output O₄O₂O₁ is a 3-bit binary count 000 ... 111, when 0, 1, ... 7 of the 7 input bits i₁...i₇ are on. For problems like this in general, you can write a boolean expression for what the BB does and a boolean expression for the desired output and then synthesize the output. In this particular case, however, try the obvious approach of putting i₁, i₂, i₃ into a first box B₁, and i₄, i₅, i₆ into a second box B₂, and i₇ into one input of a third box B₃. Looking at this it's clear that if you run the units outputs from B₁ and B₂ into the other two inputs of B₃ then the units output from B₃ is equal to the desired value O₁. You can get the sum of the twos outputs from B₁, B₂, B₃ via a box B₄, and this sum is equal to the desired values O₄O₂.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top