Contents



Going Beyond the Basics (cont'd) 

Next
 

Using Boolean Logic

A Boolean value (our Bool data type) is nothing more than a value that has only 2 possible states: TRUE or FALSE.

This turns out to be very useful for setting up decision rules for a computer to follow. The simplest case has the form,

if "A" is TRUE, then do "B"

As an example, consider the controls for a multi-waveform oscillator which only responds to the Pulse Width control when the Pulse waveform is selected. We can tap into the Boolean value of the Pulse selection and use it to control the visibility of the Pulse Width knob:



 


When Pulse is selected in the list, the value of the List to Bools' "Pulse" pin becomes TRUE, making the container's "Controls on Parent" TRUE, so the knob is displayed. If anything else is selected, the value is FALSE, and the knob in the container is hidden.

Some decisions depend on more than one Boolean variable. For instance, the DH Registration Control system can verify whether or not a synth has been validly registered, and whether or not it is still within its demo period. We would probably want to have the synth run if either of these conditions were true.

Boolean values can be combined in 2 basic ways: AND and OR.

  • The combination "A AND B" is TRUE if and only if both A and B are individually TRUE.

  • The combination "A OR B" is TRUE if and only if either A or B is true. This includes the case where both are true.

The DH_BooleanAnd and DH_BooleanOr modules combine Bool  values using AND and OR, respectively.

Below, a DH_BooleanOr is used to combine the outputs from the Registration Control system's main module. If the VST is registered or within the demo period, a positive voltage is sent to enable the synth.

Sometimes what's needed is the exact opposite of the Boolean value that's available. For that situation, we use a logical NOT. All NOT does is to give FALSE for TRUE, and TRUE for FALSE.

The DH_BooleanNot module outputs the opposite of an input Bool value.

Taking our example a step further, let's suppose we want to disable certain features for unregistered users. We could set it up like this:

Now, in addition to enabling the synth if "Registered? OR In Demo Period?" is TRUE, we send another signal that will result in turning some things off if "NOT Registered?" is TRUE.

AND, OR, and NOT can be combined in many ways to express more complex logical conditions. For example, what if, instead of the logical OR that's TRUE if either or both of its inputs are TRUE, we wanted an exclusive OR (sometimes abbreviated XOR), which is TRUE only if one, but not both of its inputs is TRUE? One way to write that in logical form is:

(A OR B) AND (NOT (A AND B))

It could be implemented like this:

Should you venture into the territory of combining logical conditions, you need to be aware of the effect that NOT has when it is applied to conditions that include AND or OR. It's very easy to get tripped up by this.

NOT (A AND B) is the same as (NOT A) OR (NOT B).
NOT (A OR B) is the same as (NOT A) AND (NOT B).

Notice how the NOT changes AND to OR, and OR to AND.

These are known as DeMorgan's Laws, and forgetting, overlooking, or misapplying them has been the cause of countless logical errors and computer program bugs.

 

top

Next