There are questions here and on electrical engeneering stackexchange with similar titles, but they do not quite address the part that is bugging me. I hope I can explain it here in a clear way and that my question is not hopelessly naive.
Suppose that I have computer program that takes two inputs: the boolean input condition and the more interesting input x. Now uppon receiving these inputs it does this:
if(condition == TRUE){
perform hideously complicated, 24 hour computation on x resulting in one-bit output y
return(y)
}else{
perform different, equally time-consuming but completely unrelated computation on x resulting in one-bit output z
return(z)
}
I assume (correct me if I'm wrong) that modern compilers have a way of making sure that they only perform the hideously long computation computing the answer (y or z) that was asked for, spending only 24 rather than 48 hours.
HOWEVER. When I try to imagine how I would built this program from AND,OR and NOT gates then I could not come up with any other way than doing this:
Take x
Split it into two copies of itself
Lead one copy through a complicated circuit computing y
Lead the other copy through a different but equally complicated circuit computing z
return: (condition AND y) OR (NOT(condition) AND z)
In other words: unless I overlook some brilliant idea, in the world of logic gates we are forced to always perform BOTH computations.
But if CPUs are built up from logic gates, how do compilers avoid always doing both computations?
