 This video is free to watch - hope you enjoy it! If you like what you see, there's a ton more where that came from...

# Logical Negation

We've covered how to add binary numbers together, but how do you subtract them? For that, you need a system for recognizing a number as negative and a few extra rules. Those rules are one's and two's complement.

This content is for subscribers only - which you can become in just 30 seconds!

This is where things get a bit weird. Binary has only two digits: a 1 and a 0, nothing else. So how do you represent a negative number? It turns out we have two ways of doing this.

## One’s Complement

In decimal you sutract a number (let’s say it’s `y`) from another number (let’s say it’s `x`) by inverting it and then peforiming addition:

``````z = x + -y
``````

Inverting a number in decimal is signified by using the `-` sign operator. This equation is a bit noisy, however, so we tend to write it in the simpler form `x-y`. We can do the exact same in binary, sort of, by inverting (or “flipping”) a binary number to get it’s complement.

Flipping is a straightforward thing: whenever you see a 1 you replace it with a 0 and vice-versa. That’s what one’s complement is: inverting and then signing a number. To “sign” a number in binary means setting the left-most bit to either a 0 (positive) or 1 (negative).

The one’s complement of a binary 1 (`01`), therefore, is `110`. It looks a bit weird and takes some getting used to, but it works.

We can now perform subtraction. Let’s try 1 - 1 in binary. Remember, the left-most digit is the sign:

``````001
110
---
111
``````

Our answer is 111, which is the binary representation of zero. Which is weird because the binary representation of zero is also `000` and, as it turns out, there are two representations of zero in one’s complement. Which sucks and is confusing.

There’s a mathematical reason why which is in the video.

## Two’s Complement

We need to do a little more work to get everything line up the way we want, which is where two’s complement comes in. It’s the same as one’s complement, but you just add one to get over that negative zero thing. Sounds kind of goofy, but it works:

``````001
110
001
---
000
``````

You’ll notice here that there’s a carry as the last operation and that, if we’re sticking to the rules, the answer should really be `1000`. Why isn’t it?

## No Last Carry in Two’s Complement

The simple reason is that we’ve already accounted for the carry in two’s complement by adding one. Nice and tidy. So: if there’s a carry, you can just ignore it.

• ### The Basics of Logic

Let’s jump right in at the only place we can: the very begining, diving into the perfectly obvious and terribly argumentative 'rules of logic'.

• ### Boolean Algebra

You're George Boole, a self-taught mathematician and somewhat of a genius. You want to know what God's thinking so you decide to take Aristotle's ideas of logic and go 'above and beyond' to include mathematical proofs.

• ### Binary Mathematics

This is a famous interview question: 'write a routine that adds two positive integers and do it without using mathematic operators'. Turns out you can do this using binary!

• ### Bitwise Operators

This is a famous interview question: 'write a routine that adds two positive integers and do it without using mathematic operators'. Turns out you can do this using binary!

• ### Logical Negation

We've covered how to add binary numbers together, but how do you subtract them? For that, you need a system for recognizing a number as negative and a few extra rules. Those rules are one's and two's complement.

• ### Entropy and Quantifying Information

Now that we know how to use binary to create switches and digitally represent information we need to ask the obvious question: 'is this worthwhile'? Are we improving things and if so, how much?

• ### Encoding and Lossless Compression

Claude Shannon showed us how to change the way we encode things in order to increase efficiency and speed up information trasmission. We see how in this video.

• ### Correcting Errors in a Digital Transmission, Part 1

There are *always* errors during the transmission of information, digital or otherwise. Whether it's written (typos, illegible writing), spoken (mumbling, environment noise) or digital (flipped bits), we have to account for and fix these problems.

• ### Functional Programming

Functional programming builds on the concepts developed by Church when he created Lambda Calculus. We'll be using Elixir for this one, which is a wonderful language to use when discovering functional programming for the first time

• ### Lambda Calculus

Before their were computers or programming languages, Alonzo Church came up with a set of rules for working with functions, what he termed lambdas. These rules allow you to compute anything that can be computed.

• ### Database Normalization

How does a spreadsheet become a highly-tuned set of tables in a relational system? There are rules for this - the rules of normalization - which is an essential skill for any developer working with data

• ### Big O Notation

Understanding Big O has many real world benefits, aside from passing a technical interview. In this post I'll provide a cheat sheet and some real world examples.

• ### Arrays and Linked Lists

The building block data structures from which so many others are built. Arrays are incredibly simple - but how much do you know about them? Can you build a linked list from scratch?

• ### Stacks, Queues and Hash Tables

You can build all kinds of things using the flexibility of a linked list. In this video we'll get to know a few of the more common data structures that you use every day.

• ### Trees, Binary Trees and Graphs

The bread and butter of technical interview questions. If you're going for a job at Google, Microsoft, Amazon or Facebook - you can be almost guaranteed to be asked a question that used a binary tree of some kind.

• ### Basic Sorting Algorithms

You will likely *never* need to implement a sorting algorithm - but understanding how they work could come in handy at some point. Interviews and workarounds for framework problems come to mind.

• ### DFS, BFS and Binary Tree Search

You now know all about trees and graphs - but how do you use them? With search and traversal algorithms of course! This is the next part you'll need to know when you're asked a traversal question in an interview. And you will be.

• ### Dynamic Programming and Fibonnaci

Dynamic programming gives us a way to elegantly create algorithms for various problems and can greatly improve the way you solve problems in your daily work. It can also help you ace an interview.

• ### Calculating Prime Numbers

The use of prime numbers is everywhere in computer science... in fact you're using them right now to connect to this website, read your email and send text messages.

• ### Graph Traversal: Bellman Ford

How can you traverse a graph ensuring you take the route with the lowest cost? The Bellman-Ford algorithm will answer this question.

• ### Graph Traversal: Djikstra

Bellman-Ford works well but it takes too long and your graph can't have cycles. Djikstra solved this problem with an elegant solution.

[[prev.summary]]

[[next.summary]]