Is there a way to use logic operators in Blender Shader nodes?
I've spent the past week or so working on basic logic gate implementations in Blender Shader editor.
I'm sharing these with the community so anyone who would like to use similar nodes can easily find how to make and use them.
They allow for a little more advanced Shader Node behavior without having to resort to OSL or python scripting.

Here's an overview of the logic gate node groups I've made, along with a .blend file that contains all of them.  

Download the .blend file here:


It contains all logic gates discussed in this topic, as well as an example of a 7-segment display and the logic that drives it. You may freely use the contents as you wish, though crediting this post and/or linking back to it would be appreciated.

To use the nodes in your files, open the file you need the nodes in and click File &gt; Append, then open the above .blend file. Navigate to "Materials" and import the Logic Node Library material. This contains all nodes mentioned in this post.

Since the node groups are protected and named, you can add them to your Shader Editor by using Shift+A &gt; Search and entering their names.



Preface  

Every logic gate can be made from Not-And or Nand gates. (This also goes for NOR, but I'm not using those).

When implementing logic gates, you first need a Nand gate; with this gate you can make any other gate using solely Nand gates. Most of the gates in this answer are solely composed of Nand gates.

All node groups have inputs that are clamped to a range of 0 to 1. Due to how the Nand gate operates, any value greater than 0 is seen as a logic 1.
The Nand gate is also the only gate that utilizes an internal value that surpasses 1; they do binary math with decimal numbers.

The outputs are also clamped to a value between 0 and 1, this makes it easy to chain node groups without losing 'signal' quality.

The terminology I'm using, just for clarification:
1 indicates that the node outputs a True signal.
0 indicates that the node outputs a False signal.  

All inputs are noted as capital letters. The output node is named O for most gates.

Outputs of Logic Gate Nodes can be fed into just about any node that accepts values. For instance, connecting the output to a Mix Shader Node fac input allows you to control which shader to use based on your logic. It's effectively a "switch" which allows you to switch between materials based on specific conditions.

The inputs of logic gates can manually be set, or controlled through keyframes.
Another possibility is to split a RGB color value into it's three component color channels and use either one of those values for logic input(since these are also ranged between 0 and 1).

If you have input values that exceed the 0 to 1 range, you can use a Map Range node to map the input range to the output range.



Basic Gates  

In this section I'll describe the construction and operation of the four basic logic gates:
- Nand
- Not
- Or
- And  

Nand  

The Nand gate outputs a 1 unless both inputs are 1, then it outputs a 0.

This is implemented as follows;


Both inputs are fed through a Math &gt; Ceil node which rounds the value up to the nearest integer. For 0 this would be 0, for anything between 0 and 1, this would be a 1.

These values are then added together using a Math &gt; Add node, which results in an internal value which is denoted as X in the following truth table:

Truth table:

Nand
A  B  |  O  X
-------------
0  0  |  1  0
0  1  |  1  1
1  0  |  1  1
1  1  |  0  2


The X value is finally passed through a Math &gt; Less Than node which outputs a 1 if the input is less than 2. If the value is 2 it will output a 0.
This node is clamped so the output value will never exceed the 0 - 1 range.

Considering every following gate consists of Nand gates that contain both input and output clamps, there is no need to further clamp these nodes.



 Or   

On to the first compound gate(a gate made from other gates).
This gate outputs a 1 when either of its inputs, or both, are 1. If all inputs are 0, it'll output a 0.

Truth table:

Or
A  B  |  O
----------
0  0  |  0
0  1  |  1
1  0  |  1
1  1  |  1


The node group consists of three Nand gates:
 



Not

This node has a pretty simple effect; it inverts the input. Is the input a 1, then it'll output a 0 and vice versa.

It is constructed from one NAND gate with both inputs linked to the group input node.


Truth table:

Not
A  |  O
----------
0  |  1
1  |  0




And  

Finally, the last basic logic gate(and perhaps one of the most useful ones): And.
This gate outputs a 1 only if both inputs are 1. Else, it'll output a 0.

Truth table:

And
A  B  |  O
----------
0  0  |  0
0  1  |  0
1  0  |  0
1  1  |  1


It consists of a Nand gate linked to a Not gate. Remember how Nand stands for Not-And? This gate effectively is Not-Not-And.




Other gates 

With these four basic gates, you can construct gates that exhibit more complex behavior.
I'll describe three gates:
- Nor
- Xnor
- Xor  



Nor  

This gate is a bit like the opposite of an And gate; its output is only 1 if all inputs are 0. Else, the output is 0.

Truth table:

Nor
A  B  |  O
----------
0  0  |  1
0  1  |  0
1  0  |  0
1  1  |  0


This gate can be used as a last option for conditions; this gate activates only if none of the inputs are active.  

It consists of an Or and a Not gate:




Xnor  

The Xnor gate outputs a 1 if both inputs are equal. Are they different? Then it'll output a 0.

Truth table:

Nor
A  B  |  O
----------
0  0  |  1
0  1  |  0
1  0  |  0
1  1  |  1


It is constructed like so:




Xor  

Finally, Xor is a gate that outputs a 1 only if both inputs are not equal. It's like the inverse of the Xnor gate.

Truth table:

Nor
A  B  |  O
----------
0  0  |  0
0  1  |  1
1  0  |  1
1  1  |  0






More complex components

In the Blender Shader engine I wouldn't expect things like binary adders and 7 segment drivers to be useful.
Singular gates operating on a few inputs are likely way more useful. I've implemented them anyway; since they're fairly complex I will not explain how they are constructed. Instead, see the attached .blend file for more information.  

Included in this file are the previously named base gates, a few gates with multiple inputs (things like And with 4 inputs), adders(half, full and 4 bit) and some groups related to handling Blender's decimal numbers.  




Here's a quick description of these gates:

The DecimalToBinary node group converts the value on a single input to a 4-bit binary value (0000 - 1111 or 0 - 15). Currently, this only actually supports numbers 0 - 9, since the 4th bit is needed to be able to count to 9. The values 10-15 are not used. I'll add this to the post as soon as I've found a solution for this.

The input value is passed through the DecToDigits node, which converts a singular value to 10 output values (Y0 - Y9).
These effectively convert a number to their digits; 4 would turn output Y4 on and leave the rest off, 8 turns Y8 on and leaves the others off, and so on.

These values are passed to the DigitToBin group which converts the digits to a 4-bit binary number (again, only 0-9 for now) using a fairly complicated node group.

In the .blend file I've also included a WIP 7-segment shader which controls a single digit based on the (keyframe-driven) input value, which is passed through the DecimalToBinary node group. Currently every digit has its own material to account for decimals and hundreds, tens, ones. I hope to find a better solution for this since every material on its own contains about 800 nodes.
The whole display encompasses about 4000 nodes!
This is also included as an example; choose Play Animation to see the display count up. This works best in Cycles render, though LookDev also works but is a lot less responsive.



In terms of performance, I haven't noticed any performance slowdowns when using a few of them here and there in my materials. In the 7-segment shader however, there are 4000 nodes in use which noticeably slows down animation speed to about 1 fps.

Again, if you only use a few of these nodes performance shouldn't be an issue.

In conclusion

Boolean logic is absolutely possible within Blender's Shader editor and may prove useful for making certain materials, like switching shaders or colors based on a keyframed value.
Things like binary computation are also possible, though that is not the focus of this post. 

I'm sure people can find interesting applications of these nodes!
I will also add examples of the basic nodes later so you can see what they can be used for.