How do you define “if” for computers?

423 views

I’m not a programmer, how do you get from binary 1s and 0s to “if x occurs complete y” How do you tell the computer to wait for a future input?

In: 2

11 Answers

Anonymous 0 Comments

There’s really two types (that I’m aware of) of IF conditions that happen in the actual circuitry.

IF zero/ IF not zero. (Commonly we see it written as if true/false) it will take a binary number and check to see if any of the digits are 1s. In such case, it will trigger an action that essentially skips to a particular line in the code, or continue on to the next line of code as normally if that trigger doesn’t get activated.

This true/false check works pretty much the same however way you do it. Just throwing in some NOT gates to get the opposite if that’s what’s desired. This can check to see if something is 0, or something isn’t 0. And I’ve never seen it done but I suppose you could have a circuit check for all 1s in the number as well. Although it’s really the exact same thing so there’s no reason for it.

If zero/not zero is also used to check if two things are equal. Just subtract one from the other, and if they’re the same you would have a zero, otherwise you’d have a non-zero value.

The other IF checks for a carryout or not after two things are added/subtracted. For example, if you add (in binary) 1010 and 1010 you get 10100. That’s really thought of as 0100 and the 1 upfront is carried out if the number has an extra digit now and doesn’t fit the standard size used in the computer.

This kind of check is generally going to be happening exclusively ‘behind the scenes’. As a computer needs to check the math it’s doing to see if it’s got the correct answer or not. Sometimes it will need to do special operations to handle a situation when there’s an unexpected result in the carryout bit.

This carry out bit can also trigger an IF statement for reasons relating to negative vs positive numbers. Good for doing things like ‘is x > y?’

You are viewing 1 out of 11 answers, click here to view all answers.