From 6eaf444bc58d08070846cebd1162ebe7925d9d04 Mon Sep 17 00:00:00 2001 From: thomasabishop Date: Wed, 20 Mar 2024 07:20:03 +0000 Subject: [PATCH] Autosave: 2024-03-20 07:20:03 --- zk/Twos_complement.md | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/zk/Twos_complement.md b/zk/Twos_complement.md index 81a89f6..8c40e2f 100644 --- a/zk/Twos_complement.md +++ b/zk/Twos_complement.md @@ -34,10 +34,38 @@ To derive the complement of an unsigned number: ![](/img/unsigned-to-signed.png) -To derive the unsigned equivalent of a signed number +To derive the unsigned equivalent of a signed number you invert the process but +still make the smallest digit `1`: + +![](/img/signed-to-unsigned.png) ### Formal expression +$$ + 2^n - x +$$ + +- where $x$ is the negative integer in binary that we wish to derive +- where $n$ is the word length of the binary system in bits. + +Applied to the earlier example we have $2^4 -5$ which is: + +$$ + 16 - 5 = 11 +$$ + +When we convert the decimal `11` to binary we get `1011` which is identical to +the signed version of the unsigned integer. + +We can confirm the correctness of the derviation by summing the signed and +unsigned binary values. If this results in zeros (ignoring the overflow bit), +the derivation is correct as the two values effectively cancel each other out: + +$$ + + 1011 + 0101 = 0000 +$$ + ## Applications ## Related notes