To summarize:
•2’s complement is used to represent negative numbers in binary.
•MSB = sign bit.
•Range: -2^(N-1) to 2^(N-1) - 1.
•Simple: invert + add 1 for negatives.
•Arithmetic is seamless!
It’s elegant and foundational to computing!
Why is 2’s complement so powerful?
Arithmetic just works.
E.g., 5 + (-3) in 4 bits:
•5 = 0101
•-3 = 1101 (2’s complement of 3)
•Add: 0101 + 1101 = 10010.
Drop the carry bit: 0010 → 2.
No special logic needed!
Let’s talk about 2’s complement, one of the most important concepts in binary arithmetic and computer science!
It’s how computers represent negative numbers and perform seamless arithmetic.
🧵
In 4-bit 2’s complement, negative numbers wrap around the circle starting at 2^4 = 16.
•-1 → 1111 (decimal: 15 in unsigned).
•-8 → 1000.
Range: -8 to 7.
This allows seamless addition/subtraction without extra rules!
How do we convert a 2’s complement binary back to decimal?
E.g., 1011 in 4 bits:
1️⃣ Check MSB: 1 → Negative.
2️⃣ Take 2’s complement:
•Invert: 1011 → 0100.
•Add 1: 0100 + 1 = 0101.
3️⃣ Result: -5.
For positives, MSB is 0—no conversion needed!
Why is it called 2’s complement?
Because subtracting a number is like adding its complement relative to 2^N.
E.g., for 5 in 4 bits, 2^4 = 16:
•Complement = 16 - 5 = 11 (binary: 1011).
This clever trick simplifies arithmetic in binary!
To find the 2’s complement (binary representation of a negative number):
1️⃣ Write the positive number in binary.
2️⃣ Invert all bits (1’s complement).
3️⃣ Add 1 to the result.
E.g., for -5 in 4 bits:
1.5 = 0101
2.Invert: 1010
3.Add 1: 1010 + 1 = 1011.
Why do we need 2’s complement?
Computers work with binary (0s and 1s).
To handle negative numbers in binary, we need a system that:
1️⃣ Represents negatives uniquely.
2️⃣ Simplifies addition/subtraction.
2’s complement solves both efficiently.
reminder that the bcrypt hash function ignores input above a certain length! so if you do bcrypt(username || password) for some reason, a sufficiently long username will make it accept any password. to fix this you can sha256 the input first.
I was just watching a video about algorithm performance by the @ThePrimeagen . It reminded that Big-O complexity is not always the final story.
When choosing a data structure, it's useful to consider the 'composition hierarchy'.
You can implement an algorithm using a Hash Table. In theory, it provides O(1) insert & lookups. But don't forget that a hash table is just implemented using a dynamic array.
With small amounts of data, a linear scan through an array is faster than hash lookups.
And if you have a fixed amount of data, why not just use a static array? (prob because you use a fake language like JS or Python)
Take it one step further. If your data can be binary encoded, you can get further performance gains. Possibly at the cost of readability.
Stepping back, you realize that this is the same principal that Protocol Buffers are built on.
Anyways, I love thinking about this stuff and thought it was worth sharing.