# Truth Tables

The aigverse library provides support for working with truth tables, which are a fundamental representation of Boolean functions. The [`TruthTable`](api/aigverse/utils/index.html.md#aigverse.utils.TruthTable) class offers efficient manipulation and analysis of Boolean functions.

#### NOTE
Truth tables provide a complete specification of a Boolean function by listing all possible input combinations and their corresponding outputs. They are particularly useful for small functions where exhaustive enumeration is feasible.

## Creating Truth Tables

Truth tables can be created in several ways:

```ipython3
from aigverse.utils import TruthTable

# Initialize a truth table with 3 variables (2^3 = 8 entries)
tt = TruthTable(3)

# Create a truth table from a hex string (representing the Majority function)
tt.create_from_hex_string("e8")
print(f"Truth table from hex string: {tt.to_binary()}")

# Create a truth table for an AND function
tt_and = TruthTable(2)
tt_and.create_from_hex_string("8")
print(f"AND function: {tt_and.to_binary()}")

# Create a truth table for an OR function
tt_or = TruthTable(2)
tt_or.create_from_hex_string("e")
print(f"OR function: {tt_or.to_binary()}")
```

```myst-ansi
Truth table from hex string: 11101000
AND function: 1000
OR function: 1110
```

## Basic Manipulation

Truth tables provide various methods for bit manipulation:

```ipython3
# Create a truth table
tt = TruthTable(3)
tt.create_from_hex_string("e8")  # Majority function

# Get individual bits
print(f"Original truth table: {tt.to_binary()}")
print(f"Bit at position 0: {int(tt.get_bit(0))}")
print(f"Bit at position 7: {int(tt.get_bit(7))}")

# Flip bits
tt.flip_bit(0)
tt.flip_bit(7)
print(f"After flipping bits 0 and 7: {tt.to_binary()}")

# Clear the truth table
tt.clear()
print(f"After clearing: {tt.to_binary()}")

# Check if constant
print(f"Is constant 0? {tt.is_const0()}")
```

```myst-ansi
Original truth table: 11101000
Bit at position 0: 0
Bit at position 7: 1
After flipping bits 0 and 7: 01101001
After clearing: 00000000
Is constant 0? True
```

## Truth Table Properties

You can analyze various properties of truth tables:

```ipython3
# Create a truth table for XOR
tt_xor = TruthTable(2)
tt_xor.create_from_hex_string("6")  # XOR function
print(f"XOR function: {tt_xor.to_binary()}")

# Get number of variables and bits
print(f"Number of variables: {tt_xor.num_vars()}")
print(f"Number of bits: {tt_xor.num_bits()}")

# Check if the function is balanced (equal number of 0s and 1s)
num_ones = sum(int(tt_xor.get_bit(i)) for i in range(tt_xor.num_bits()))
is_balanced = num_ones == tt_xor.num_bits() // 2
print(f"Is balanced? {is_balanced}")
```

```myst-ansi
XOR function: 0110
Number of variables: 2
Number of bits: 4
Is balanced? True
```

## Truth Table Simulation

The simulation of AIGs and other logic networks using truth tables is covered in the [Simulation section](algorithms.html.md#simulation) of the Algorithms documentation. This approach allows you to obtain the truth tables for outputs and internal nodes of a logic network.

## `pickle` Support

Truth tables support Python’s `pickle` protocol, allowing you to serialize and deserialize them for persistent storage or use in data science workflows.

```ipython3
import pickle

# Create a truth table
tt = TruthTable(3)
tt.create_from_hex_string("d8")  # ITE function

# Pickle the truth table
with open("tt.pkl", "wb") as f:
    pickle.dump(tt, f)

# Unpickle the truth table
with open("tt.pkl", "rb") as f:
    unpickled_tt = pickle.load(f)

# Verify that the unpickled object is identical
print(f"Original:    {tt.to_binary()}")
print(f"Unpickled:   {unpickled_tt.to_binary()}")
print(f"Equivalent:  {tt == unpickled_tt}")
```

```myst-ansi
Original:    11011000
Unpickled:   11011000
Equivalent:  True
```

You can also pickle multiple truth tables at once by storing them in a list or tuple.
