# Half adder

> An XOR gate for the sum and an AND gate for the carry, with TikZ's US-style logic symbols. A Digital logic example with its LaTeX source.

A Digital logic example in the TeXeR TikZ library: https://texer.org/library/digital-logic/half-adder

Package: [Digital logic](https://texer.org/library/digital-logic.md), on CTAN at https://ctan.org/pkg/pgf.

Tags: half adder, logic gates, XOR, arithmetic circuit

## Source

```latex
\usetikzlibrary{circuits.logic.US}

\begin{tikzpicture}[circuit logic US, thick, every circuit symbol/.style={thick}]
  \node[xor gate] (sum) at (3,1) {};
  \node[and gate] (carry) at (3,-1) {};

  \draw (sum.input 1) -- ++(left:14mm) coordinate (a1);
  \draw (carry.input 1) -- ++(left:14mm) coordinate (a2);
  \draw (sum.input 2) -- ++(left:8mm) coordinate (b1);
  \draw (carry.input 2) -- ++(left:8mm) coordinate (b2);
  \draw (a1) -- (a2) (b1) -- (b2);
  \draw (a1) -- ++(left:8mm) node[left] {$A$};
  \draw (b2) -- ++(left:14mm) node[left] {$B$};
  \fill (a1) circle (1.5pt) (b2) circle (1.5pt);

  \draw (sum.output) -- ++(right:8mm) node[right] {$S = A \oplus B$};
  \draw (carry.output) -- ++(right:8mm) node[right] {$C = AB$};
\end{tikzpicture}
```

## As a complete document

The same example in a document that compiles on its own with pdfLaTeX:

```latex
\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{amssymb}
\usetikzlibrary{circuits.logic.US}

\begin{document}

\begin{tikzpicture}[circuit logic US, thick, every circuit symbol/.style={thick}]
  \node[xor gate] (sum) at (3,1) {};
  \node[and gate] (carry) at (3,-1) {};

  \draw (sum.input 1) -- ++(left:14mm) coordinate (a1);
  \draw (carry.input 1) -- ++(left:14mm) coordinate (a2);
  \draw (sum.input 2) -- ++(left:8mm) coordinate (b1);
  \draw (carry.input 2) -- ++(left:8mm) coordinate (b2);
  \draw (a1) -- (a2) (b1) -- (b2);
  \draw (a1) -- ++(left:8mm) node[left] {$A$};
  \draw (b2) -- ++(left:14mm) node[left] {$B$};
  \fill (a1) circle (1.5pt) (b2) circle (1.5pt);

  \draw (sum.output) -- ++(right:8mm) node[right] {$S = A \oplus B$};
  \draw (carry.output) -- ++(right:8mm) node[right] {$C = AB$};
\end{tikzpicture}
\end{document}
```
