# Combinational Adders, Carry-Lookahead & Bitwise Manipulation Logics (Animated)
Exam: GATE Computer Science & Information Technology (GATE CS)
Subject: Digital Logic
Canonical URL: https://gateandnet.com/exams/gate-cs/digital-logic/adders-carry-lookahead-and-bitwise-logics-animated

> Interactive 1-bit Full-Adder gate signal simulator, Ripple-Carry vs Carry-Lookahead (Generate/Propagate) delay formulas, Flip-Flop excitation tables, and O(1) C bitwise manipulation logics.

## 1. Interactive 1-Bit Full Adder & Universal Gate Signal Simulator

Toggle inputs **A**, **B**, and **Cin** below to trace live binary signals through the two Half-Adder stages and check universal `NAND` / `NOR` gate outputs:

```visualizer
logic-circuit
```

## 2. Adder Gate Counts & Propagation Delays (Classic GATE NATs)

- **Half Adder:** $S = A \oplus B,\; C = AB$ $\implies$ **5** 2-input NAND gates (or **5** 2-input NOR gates).
- **Full Adder:** Built from **2 Half Adders + 1 OR gate** $\implies$ minimum **9** 2-input NAND gates (or **9** 2-input NOR gates!).
- **$n$-Bit Ripple-Carry Adder Delay:** Each full adder carry path has $2$ gate delays, so worst-case $C_n$ takes $2n$ gate delays (and final sum $S_{n-1}$ takes $(2(n-1) + \text{XOR})$ depending on implementation).

## 3. Flip-Flop Characteristic & Excitation Table Reference

| Flip-Flop | Characteristic Equation $Q(t+1)$ | $0 \to 0$ | $0 \to 1$ | $1 \to 0$ | $1 \to 1$ |
| :--- | :--- | :---: | :---: | :---: | :---: |
| **SR** ($S \cdot R = 0$) | $S + \overline{R}\,Q$ | $S=0, R=X$ | $S=1, R=0$ | $S=0, R=1$ | $S=X, R=0$ |
| **JK** | $J\overline{Q} + \overline{K}Q$ | $J=0, K=X$ | $J=1, K=X$ | $J=X, K=1$ | $J=X, K=0$ |
| **D (Delay)** | $D$ | $D=0$ | $D=1$ | $D=0$ | $D=1$ |
| **T (Toggle)** | $T \oplus Q$ | $T=0$ | $T=1$ | $T=1$ | $T=0$ |

## 4. Exam-Favorite Bitwise Logic Idioms in C

1. **Find unique element when every other element appears twice:** `ans ^= A[i]` ($x \oplus x = 0,\; x \oplus 0 = x$, commutative & associative!).
2. **Swap without temporary (when `&a != &b`):** `a ^= b; b ^= a; a ^= b;`
3. **Hamming Distance between integers $x, y$:** Count set bits (`popcount`) in `x ^ y`.

## Solved Practice Questions & PYQs

### [GATE CS 2021 Set-1 | NAT]
How many times does the `while` loop body execute for `unsigned int x = 2024;` in `int c = 0; while (x) { x = x & (x - 1); c++; }`?
**Correct Answer:** 7
**Solution:** Brian Kernighan’s `x &= (x - 1)` clears the lowest set `1`-bit per iteration! Since $2024 = 2048 - 24 = 1024 + 512 + 256 + 128 + 64 + 32 + 8 = 2^{10}+2^9+2^8+2^7+2^6+2^5+2^3$, binary `11111101000` has exactly **7** set bits!
