Everything on the site.

Big-O notationAlgorithms · Count the steps in real loops, then compare n, n log n, n² and 2ⁿ on a live chart.RecursionAlgorithms · Watch frames push and pop on the call stack for factorial and Fibonacci.BFS vs DFSAlgorithms · A queue against a stack: draw a graph and see which order each search visits it in.Sorting algorithmsAlgorithms · Seven sorts on the same input, with comparison, swap and write counts side by side.Binary searchAlgorithms · Halve the range, probe the middle, repeat: with lo, mid and hi drawn on the array.Sets, relations and functionsDiscrete maths · Shade any set expression on a Venn diagram and test a relation for its properties.Proof by inductionDiscrete maths · Four worked proofs revealed one step at a time, and a broken one to diagnose.CombinatoricsDiscrete maths · An exact nPr and nCr calculator, and a Pascal’s triangle that shows its own addition rule.Boolean algebraDiscrete maths · The laws, De Morgan, and normal forms, with a truth-table generator built in.Finite-state machinesTheory · Build a DFA in a table, see it drawn, and run strings through it one symbol at a time.Domain theoryTheory · Why recursion needs least fixed points, and how CPOs, Scott continuity and powerdomains supply them.Truth table generatorTools · Expression in, truth table out: with DNF, CNF, a minimal form and LaTeX.Graph traversal visualiserTools · Draw a graph, then animate BFS, DFS and Dijkstra with the frontier shown.Recursion tree visualiserTools · Paste a recursive function or a recurrence and see its call tree and call count.Gradient descentML maths · Drag the starting point, set the learning rate, and watch it converge or blow up.Vectors and embeddingsML maths · Bend the plane with a 2×2 matrix and measure how close toy word vectors are.Softmax and cross-entropyML maths · Edit the logits and watch the probabilities, the loss and the gradient update live.Attention mechanismML maths · Q, K and V for three tokens, a live score heatmap, and the weighted sum of values.Image to video attentionML maths · One photo, 16 frames: switch reference attention on or off and watch the colours hold or drift.Formatting a CS paper in LaTeXLaTeX · Page limits, \documentclass, front matter, theorem environments and the bibliography.Camera-ready checklistLaTeX · Fonts embedded, PDF/A, copyright form, source bundle, and why final versions get rejected.LaTeX templatesLaTeX · Where the official classes live, plus a minimal article template you can copy.Boolean algebra calculatorTools · A Boolean expression simplifier that names the law used at every step.K-map solverTools · Fill in a Karnaugh map and see the groups, the minimal SOP and POS, and the essential terms.Logic gate simulatorTools · Wire up gates, flip the switches, and get the truth table and expression of your circuit.Regex to NFA converterTools · Type a regular expression and watch Thompson’s construction build its NFA, fragment by fragment.NFA to DFA converterTools · Enter an NFA or a regex and step through the subset construction, then minimise the DFA.DFA simulatorTools · Edit a DFA, run strings through it step by step, batch-test them and share the machine as a link.Regular expressions to automataTheory · Regex to NFA to DFA to minimal DFA, worked through on one example with every set shown.The halting problemTheory · Why no program can tell whether every program halts, with the paradox program played out.Pigeonhole principleDiscrete maths · n pigeons, k holes: why some hole gets ⌈n/k⌉, and the surprising things that follows from it.Dijkstra’s algorithmAlgorithms · Shortest paths with a priority queue: a worked table, a proof, and a graph you can redraw.Hash functionsAlgorithms · Keys in, buckets out: hash a word four ways, then watch chaining and probing handle collisions.Sorting algorithm visualizerTools · Up to seven sorts race on the same data, then step through any one of them line by line.LaTeX table generatorTools · Type or paste a table, merge cells, pick booktabs, and copy clean tabular code.Combination calculatorTools · Exact nCr with the cancelling shown, combinations with repetition, and a full list for small n.Permutation calculatorTools · nPr, nʳ, anagrams like MISSISSIPPI and round-table seatings, with exact working.AVL tree visualizerTools · Insert, delete and search, then replay every balance factor and rotation step by step.IEEE 754 converterTools · Decimal to float bits and back, with the exact stored value and why 0.1 isn’t exact.Two’s complement calculatorTools · Signed integers to bits and back, with the working, other encodings and overflow checks.Power set calculatorTools · Every subset of your set, sorted by size or bitmask, with 2ⁿ counted exactly.Huffman coding calculatorTools · Text or frequencies in, Huffman tree and codes out, with every merge shown in order.LaTeX matricesLaTeX · pmatrix, bmatrix, augmented and large matrices, each with copyable code and its result.AlgorithmsSection · Step-through visualisations of sorting, searching, recursion, graph traversal, Dijkstra’s algorithm, hash functions and Big-O growth, with pseudocode that follows along.Discrete mathsSection · Sets, relations and functions, proof by induction, counting, the pigeonhole principle and Boolean algebra, explained with worked examples and interactive checkers.TheorySection · Finite-state machines, regular expressions and automata, the halting problem, and the mathematical semantics of programming languages, explained from first principles.ML mathsSection · Gradient descent, vectors and embeddings, softmax, attention and image to video models, each with a formula, a worked numeric example and a live version.ToolsSection · Free browser tools: truth tables, K-maps, logic gates, nCr, power sets, regex to DFA, sorting, graph and AVL tree visualisers, IEEE 754, two’s complement and Huffman codes.LaTeXSection · How to format a CS conference or workshop paper in LaTeX, a camera-ready checklist, a minimal template you can adapt, and how to typeset matrices.
Free tool

Two’s complement calculator: decimal, binary and hex

Convert a signed decimal number to two’s complement binary and hex, or decode a bit pattern back to decimal, at 4, 8, 16, 32 or 64 bits. The calculator shows the invert-and-add-one working, compares sign-magnitude and ones’ complement, and adds two numbers with the carries and overflow check drawn out.

Interactive: Two’s complement converter and adder with clickable bitsUpdated

Two’s complement calculator

Bits (click a bit to flip it; the red one is the sign bit)

Step-by-step working

    What two’s complement is

    Computers store integers in a fixed number of bits. With nn bits there are 2n2^n patterns, and an unsigned integer uses them for 0 to 2n−12^n - 1. To represent negative numbers too, almost every processor built since the 1970s uses two’s complement: the patterns are the same, but the top bit gets a negative weight.

    value(bn−1bn−2…b0)=−bn−1 2n−1+∑i=0n−2bi 2i.\text{value}(b_{n-1} b_{n-2} \dots b_0) = -b_{n-1}\, 2^{n-1} + \sum_{i=0}^{n-2} b_i\, 2^{i}.

    In 8 bits the weights are −128, 64, 32, 16, 8, 4, 2, 1. So 01111111 is 127, the largest value, and 10000000 is −128, the smallest. 11111111 is −128 + 127 = −1. A leading 1 always means the number is negative, which is why the top bit is called the sign bit, but unlike sign-magnitude the other bits are not simply the size of the number.

    The range of an nn-bit two’s complement integer is

    −2n−1  ≤  x  ≤  2n−1−1.-2^{n-1} \;\le\; x \;\le\; 2^{n-1} - 1.
    WidthTypical typeMinimumMaximum
    8 bitsint8_t, byte (Java)−128127
    16 bitsint16_t, short−32,76832,767
    32 bitsint32_t, int−2,147,483,6482,147,483,647
    64 bitsint64_t, long−9,223,372,036,854,775,8089,223,372,036,854,775,807

    Converting a negative number: invert and add one

    Worked example: −18 in 8 bits

    1. Write the magnitude, 18, in binary and pad to 8 bits: 0001 0010.
    2. Invert every bit (this is the ones’ complement): 1110 1101.
    3. Add 1: 1110 1110. That is −18, or 0xEE in hex.
    4. Check with the weights: −128 + 64 + 32 + 8 + 2 = −18. ✓

    Why does this work? Inverting xx in nn bits gives (2n−1)−x(2^n - 1) - x, so adding one gives 2n−x2^n - x. That is the second method: a negative number −x-x is stored as the unsigned number 2n−x2^n - x. For −18 in 8 bits, 256−18=238256 - 18 = 238, and 238 in binary is 1110 1110. Negation works the same way in both directions: invert and add one to 1110 1110 and you get 18 back.

    A quick shortcut by hand: copy bits from the right up to and including the first 1, then invert everything to its left. For 18 = 0001 0010, keep 10, flip the rest to get 1110 1110.

    Converting two’s complement to decimal

    If the top bit is 0, read the number as ordinary binary. If it is 1, either add up the weights with the top one negative, or read the pattern as unsigned and subtract 2n2^n. For 1010 0110: unsigned it is 166, and 166−256=−90166 - 256 = -90. Or invert and add one to get the magnitude: 0101 1010 = 90, so the value is −90. Choose “Binary or hex → decimal” above to see each step for your own pattern. Hex works too: 0xFF in 8 bits is −1, and 0x8000 in 16 bits is −32,768.

    Changing width keeps the value only if you sign-extend: copy the sign bit into the new high bits. −18 in 16 bits is 1111 1111 1110 1110, not 0000 0000 1110 1110 (which is 238).

    Why computers use two’s complement

    There are three classic ways to store signed integers. Here are all of them in 4 bits:

    ValueTwo’s complementOnes’ complementSign-magnitude
    −81000——
    −7100110001111
    −6101010011110
    −5101110101101
    −4110010111100
    −3110111001011
    −2111011011010
    −1111111101001
    0000000000000
    1000100010001
    2001000100010
    3001100110011
    4010001000100
    5010101010101
    6011001100110
    7011101110111

    Sign-magnitude and ones’ complement both waste a pattern on a second zero (1000 and 1111 respectively are “−0”), and both need special cases in the adder. Two’s complement has these advantages:

    • One zero, so it represents one extra negative number: −8 in 4 bits, −128 in 8.
    • The same adder works for signed and unsigned numbers. Addition is just binary addition modulo 2n2^n, so the hardware doesn’t need to know which kind it is adding.
    • Subtraction is addition: a−b=a+(¬b)+1a - b = a + (\lnot b) + 1, so a subtractor is an adder with inverters and a carry-in of 1.
    • Comparison with zero just looks at the top bit, and multiplication’s low half is the same as for unsigned numbers.

    The one odd value is the minimum. −128 has no positive partner in 8 bits, so negating it gives −128 again. That is why abs(INT_MIN) overflows in C and Java.

    Addition and overflow

    Add two’s complement numbers exactly as unsigned binary and keep the low nn bits. For example, −7 + 5 in 8 bits gives 1111 1110 = −2, correct with no special handling. The result is wrong only when the true sum is outside the range, which is called overflow:

    The overflow rule Adding two numbers with the same sign overflows exactly when the result has the opposite sign. Adding numbers with different signs can never overflow. The carry out of the top bit does not indicate signed overflow; it signals unsigned overflow instead.

    100 + 50 in 8 bits gives 1001 0110, which reads as −106: two positive numbers produced a negative result, because the true sum 150 is above 127. Try it in “Add two numbers”. In hardware, overflow is the XOR of the carry into the sign bit and the carry out of it. In C and C++ signed overflow is undefined behaviour, so compilers may assume it never happens; Java and C# wrap round silently, and Rust panics in debug builds.

    Related

    References

    1. R. E. Bryant and D. R. O’Hallaron, Computer Systems: A Programmer’s Perspective, 3rd ed., Pearson, 2016. Sections 2.2–2.3.
    2. D. A. Patterson and J. L. Hennessy, Computer Organization and Design: The Hardware/Software Interface, RISC-V ed., Morgan Kaufmann, 2017. Section 2.4.
    3. D. E. Knuth, The Art of Computer Programming, Volume 2: Seminumerical Algorithms, 3rd ed., Addison-Wesley, 1997. Section 4.1.