DEV Community

Cover image for Binary for Beginners: The ABCs of 0s and 1s
Aleksandr Hovhannisyan
Aleksandr Hovhannisyan

Posted on • Edited on • Originally published at aleksandrhovhannisyan.com

Binary for Beginners: The ABCs of 0s and 1s

Note: This post was originally published on my dev blog. Also, it's really long, lol. My aim was to create the most comprehensive tutorial on binary numbers—one that I wish I had back when I was learning this stuff. I hope someone out there finds this helpful!

What is 10? If this is your first time learning about the binary number system, then this question may seem odd. Of course it's ten, right?

Let's try something different. Have you ever heard this joke?

There are 10 types of people in the world: those who understand binary and those who don't.

Unless you're familiar with binary numbers, this probably doesn't make much sense. But by the end of this post, you'll come to appreciate this and many other awful developer jokes!

In this beginner's tutorial, we'll look at everything you need to know about the binary number system, but we'll also take a quick look at decimal and hexadecimal, as they're closely related. I'll include relevant bits of code and real-life examples to help you appreciate the beauty of binary.

Table of Contents

What Is a Number System?

Before we look at binary, let's take a step back and discuss number systems in general.

Now, it may be strange to think of number "systems" in the plural if this is your first time learning about them. That's because the majority of the world is familiar with just one: the decimal number system (aka "base ten"), also known as the Arabic number system. This system has digits ranging from 0 to 9, which we use to form numbers in our daily lives.

For example, in the decimal number system, 579 expands to this:

579 = 5(102) + 7(101) + 9(100) = 500 + 70 + 9

As a kid, you were taught that the 5 in 579 is in the "hundreds place," the 7 is in the "tens place," and the 9 is in the ones place. Notice that the 5 is multiplied by one hundred (102), the 7 by ten (101), and the 9 by one (100) to form the decimal number 579. Makes sense, right?

Here, the number 10 is what we call the base (aka radix) of our number system. Notice the powers of 10 in the expanded expression above: 102, 101, and 100. For this reason, the terms decimal and base 10 are interchangeable.

In the decimal number system, a number is represented by placing digits into "buckets" that represent increasing powers of ten, starting with 100 in the rightmost "bucket," followed by 101 to its immediate left, and so on infinitely:

Increasing powers of ten

Any unused buckets to the far left have an implicit value of 0 in them. We usually trim leading zeros because there is no use in saying 00579 when that's mathematically identical to 579.

Why did humans pick 10 to be the base of their preferred number system? Most likely because we're born with ten fingers and ten toes, and we're used to counting with our fingers when we're young. So it's simply natural for us to use this number system!

Bases, Exponents, and Digits

As I've already hinted, the decimal number system (base 10) isn't the only one in existence. Let's use a more general mathematical notation to represent number systems beyond just our familiar one.

In a number system with a fixed base of b, the available digits range from 0 to b - 1. For example, in the decimal number system (b = 10), we can only use the digits 0, 1, 2, ..., 9. When you "run out" of digits in a single bucket, you carry over a one to the next power of the base. For example, to get to the number after 99, you carry a one to the next power of ten: 100.

Now, suppose that we have a string of digits d1 d2 ... dn (where n is just the number of digits). Maybe that's d1 d2 ... dn = 579 from our earlier example. That string expands like this:

d1bn-1 + d2bn-2 + ... + dnb0

And you can visualize that like this:

A generic base of b with digits d

Using our same example, d1bn-1 + d2bn-2 + ... + dnb0 = 5(102) + 7(101) + 9(100)

Expanding 579 in terms of powers of 10

Again, we have buckets from right to left in increasing powers of our base (10).

Note: The rightmost bucket will always represent dn in any number system. Why? Because any base raised to the power of 0 is just 1.

Now, in reality, you can have a number system that uses a base of 2, 3, 4, 120, and so on. Some of these have special names because they're used more often than others:

Base Name Description
1 Unary Also known as tallying. A number n is represented by picking an arbitrary character and repeating it n times (e.g., xxxx would be 4).
2 Binary Only two digits: zero and one. Most commonly used in computing. Everything on a computer is, at the lowest possible level, stored using the binary number system.
8 Octal Only eight digits are available: 0–7.
16 Hexadecimal Fifteen digits: 0–9 and a–f. Often used to express binary strings more compactly.
60 Sexagesimal How many seconds are in a minute? How many minutes in an hour? This is the basis of the modern circular coordinate system (degrees, minutes, and seconds).

For this reason, when discussing number systems, we usually subscript a number with its base to clarify its value. Alternatively, you can prepend a number with a certain string (usually 0b for binary or 0x/# for hexadecimal). So we'd write 579 as 57910, or the binary number 1001 as 10012 (or 0b1001). Otherwise, if we were to merely write the number 1001 without providing any context, nobody would know whether that's in binary, octal, decimal, hexadecimal, and so on because the digits 0 and 1 are valid in all of those number systems, too!

Note: When not comparing number systems, we usually assume that a given number is in decimal unless otherwise noted, and thus the subscript is omitted.

The Binary Number System (Base 2)

So far so good—we're all familiar with decimal numbers because we use them everyday. But what's the deal with the binary number system?

By definition, the binary number system has a base of 2, and thus we can only work with two digits to compose numbers: 0 and 1. Technically speaking, we don't call these digits—they're called bits in binary lingo.

Each "bucket" in a binary string represents an increasing power of two: 20, 21, 22, and so on.

The binary number system uses powers of two

The leftmost bit is called the most significant bit (MSB), while the rightmost bit is called the least significant bit (LSB).

Here are some examples of representing decimal numbers in the binary number system:

  • Zero: 010 = 02. Expansion: 0 (20)
  • One: 110 = 12. Expansion: 1(20)
  • Two: 210 = 102. Expansion: 1(21) + 0(20)
  • Three: 310 = 112. Expansion: 1(21) + 1(20)
  • Four: 410 = 1002. Expansion: 1(22) + 0(21) + 0(20)
  • Five: 510 = 1012. Expansion: 1(22) + 0(21) + 1(20)

Note: Like in the decimal number system, leading zeros are usually stripped from binary strings. The only exception is if you're working with a signed binary number system, where a leading zero indicates that a number is positive and a leading one indicates that it's negative.

Having learned the binary number system, you should now understand the joke from earlier:

There are 10 types of people in the world: those who understand binary and those who don't.

Here, we really mean the binary equivalent of two, which looks like ten to our eyes when it's not properly subscripted: 102 = 1 × 21 = 210.

Binary Is Close to the Hardware of a Computer

Why do we bother with using the binary number system in the first place? Doesn't it seem like a whole lot of extra work to represent numbers in this manner when we could instead use the decimal number system? Well, yes—if you're writing these out by hand, it's certainly more work to represent (and manipulate) binary numbers.

You may not see any point in using binary if you haven't learned about computer architecture at a low level. Internally, computers are nothing more than electrical circuits tied to hardware. Current either flows through a wire or doesn't—a binary state. Likewise, computers use logic gates (AND/OR/NOR/XOR) to control the flow of a program's execution, and these take binary inputs (true/false). The best way to represent these low-level interactions is to use the binary number system: 0 means OFF (or false in its logical form) and 1 means ON (or true).

Note: If the whole world were to agree to it, we could just as well instead treat 0 as ON and 1 as OFF. However, it just makes more sense to treat 0 as OFF/false—after all, zero denotes the absence of a value. Hence, it's a natural candidate for representing things like falsehood or the lack of a current flowing through a wire.

Everything on your computer—the files you save and the software you install—is represented as nothing more than zeros and ones. But how is this possible?

The ASCII Standard

Suppose you create a file on your computer and store some basic text in it:

echo Hello, Binary > file
Enter fullscreen mode Exit fullscreen mode

At the end of the day, your computer can't store a character like H, e, l, or o (or even the space between two words) literally. Computers only know how to work with binary. Thus, we need some way to convert these characters to numbers. And that's why the ASCII standard was introduced.

Formally, ASCII is referred to as a character encoding standard. Put more simply, it's a method of representing human-readable characters like H, e, ,, ?, and 9 numerically so that computers can understand and use them like we do.

Here is a typical ASCII chart that you may have seen before:

An ASCII table showing characters and their numerical representations

In the ASCII standard, there are a total of 128 characters, each mapped to a unique number in binary (with an equivalent representation in decimal that we humans understand more naturally):

  • Arabic digits: 0-9 (10)
  • Capital letters of the English alphabet: A-Z (26)
  • Lowercase letters of the English alphabet: a-z (26)
  • Punctuation and special characters (66)

1 Character = 1 Byte

In the decimal number system, we're used to working with digits. In binary, as we already saw, we're used to working with bits. There's another special group of digits in binary that's worth mentioning: A sequence of eight bits is called a byte.

Here are some examples of valid bytes:

00000000
10000000
11101011
11111111
Enter fullscreen mode Exit fullscreen mode

... and any other valid permutation of eight 0s and 1s that you can think of.

Why is this relevant? Because on modern computers, characters are represented using bytes.

Recall that the ASCII standard needs to support a total of 128 characters. So how many unique number can we represent with 8 bits (a byte)?

Well, using the product rule from combinatorics, we have eight "buckets," each with two possible values: either a 0 or a 1. Thus, we have 2 × 2 × ... × 2 = 28 possible values.

In decimal, this is 28 = 256 possible values. By comparison, 27 = 128. And 128 happens to be the number of characters that we want to represent.

So... That's weird, and seemingly wasteful, right? Why do we use 8 bits (one byte) to represent a character when we could use 7 bits instead and meet the precise character count that we need?

Good question! We use bytes because it's not possible to evenly divide a group of 7 bits, making certain low-level computations difficult if we decide to use 7 bits to represent a character. In contrast, a byte can be evenly split into powers of two:

11101011
[1110][1011]
[11][10][10][11]
Enter fullscreen mode Exit fullscreen mode

The key takeaway here is that we only need one byte to store one character on a computer. This means that a string of five characters—like Hello—occupies five bytes of space, with each byte being the numerical representation of the corresponding character per the ASCII standard.

Remember the file we created earlier? Let's view its binary representation using the xxd Unix tool:

xxd -b file
Enter fullscreen mode Exit fullscreen mode

The -b flag stands for binary. Here's the output that you'll get:

00000000: 01001000 01100101 01101100 01101100 01101111 00101100  Hello,
00000006: 00100000 01000010 01101001 01101110 01100001 01110010   Binar
0000000c: 01111001 00001010                                      y.
Enter fullscreen mode Exit fullscreen mode

The first line shows a sequence of six bytes, each corresponding to one character in Hello,.

Let's decode the first two bytes using our knowledge of the binary number system and ASCII:

  • 01001000 = 1(26) + 1(23) = 7210. Per our ASCII table, this corresponds to H.
  • 01100101 = 1(26) + 1(25) + 1(22) + 1(20) = 10110, which is e in ASCII.

Cool! Looks like the logic pans out. You can repeat this for all of the other bytes as well. Notice that on the second line, we have a leading space (from Hello, Binary), represented as 25 = 3210 in ASCII (which is indeed Space per the lookup table!).

By the way, what's up with the numbers along the left-hand side of the output? What does 0000000c even mean? Time to explore another important number system!

The Hexademical Number System (Base 16)

As I mentioned in the table from earlier, the hexadecimal number system is closely related to binary because it's often used to express binary numbers more compactly, instead of writing out a whole bunch of zeros and ones.

The hexadecimal number system has a base of 16, meaning its digits range from 0–15.

Note: In technical terms, a hexadecimal digit is called a nibble, but you'll commonly hear people just call them "hex digits."

This is our first time encountering a number system whose digits are made up of more than two characters. How do we squeeze 10, 11, or 15 into a single "bucket" or "slot" for a digit? To be clear, this is perfectly doable if you have clear delimiters between digits. But in reality, that's not practical.

Let's take a step back and consider a simple hexadecimal number:

0x42

What does this mean to us humans in our decimal number system? Well, all we have to do is multiply each digit by its corresponding power of 16:

0x42 = 4(161) + 2(160) = 6410 + 210 = 6610

Okay, so that's a simple hex number. Back to the problem at hand: How do we represent the hex digits 10, 11, and so on? Here's an example that's pretty confusing unless we introduce some alternative notation:

0x15

Is this a 15 in a single slot or a 1 and a 5 in two separate slots? One way to make this less ambiguous is to use some kind of delimiter between slots, but again, that's not very practical:

0x8[15]29

The better solution that people came up with is to map 10–15 to the the English letters a–f.

Note: Capitalization doesn't matter, so you can use a-f or A-F. Just be consistent.

Here's an example of a hexadecimal number that uses one of these digits:

0xf4

And here's its expansion:

0xf4 = 15(161) + 4(160) = 24010 + 410 = 24410

There's nothing magical about the hexadecimal number system—it works just like unary, binary, decimal, and others. All that's different is the base!

Before we move on, let's revisit the output from earlier when we used xxd on our sample file:

00000000: 01001000 01100101 01101100 01101100 01101111 00101100  Hello,
00000006: 00100000 01000010 01101001 01101110 01100001 01110010   Binar
0000000c: 01111001 00001010                                      y.
Enter fullscreen mode Exit fullscreen mode

The numbers along the left-hand side mark the starting byte for each line of text on the far right. For example, the first line of text (Hello,) ranges from byte #0 (H) to byte #5 (,). The next line is marked as 00000006, meaning we're now looking at bytes #6 through 11 (B to r). Finally, the last label should make sense now that you know the hexadecimal number system: c maps to 12, meaning the byte that follows corresponds to the twelfth character in our file.

How to Convert Between Binary and Hexadecimal

Now that we know a bit about binary and hexadecimal, let's look at how we can convert between the two systems.

Binary to Hexadecimal

Say you're given this binary string and you'd like to represent it in hexadecimal:

011011100101

While at first this may seem like a pretty difficult task, it's actually very easy.

Let's do a bit of a thought exercise:

In the hexadecimal number system, we have 16 digits from 0 to 15. Over in binary land, how many bits do we need to represent these 16 values?

The answer is four because 24 = 16. With four "buckets," we can create the numbers zero (0000), one (0001), ten (1010), all the way up to fifteeen (1111).

This means that when you're given a binary string, all you have to do is split it into groups of four bits and evaluate them to convert binary to hexadecimal!

011011100101
[0110][1110][0101]
6 14 5
Enter fullscreen mode Exit fullscreen mode

Now we just replace 10–15 with a-f and we're done: 0x6e5.

Hexadecimal to Binary

What about the reverse process? How do you convert a hexadecimal number to binary?

Say you're given the hexadecimal number 0xad. What do we know about each hexadecimal digit? Well, from our earlier thought exercise, we know that four bits = one hex digit. So now we just have to convert each indiviual digit to its 4-bit binary representation and then stick each group together!

0xad = 0b10101101

Super easy, just like I promised.

Real-World Application: Representing Colors with RGB/Hex

While we're on the topic of binary and hexadecimal, it's worth taking a look at one real-world use case for the things we've learned so far: RGB and hex colors.

Colors have three components: red, green, and blue (RGB). With LED (light-emitting diode) displays, each pixel is really split into these three components using a color diode. If a color component is set to 0, then it's effectively turned off. Otherwise, its intensity is modulated between 0 and 255, giving us a color format like rgb(0-255, 0-255, 0-255).

Let's consider this hex color: #4287f5. What is it in the RGB format?

Well, we need to split this hex string evenly between red, green, and blue. That's two digits per color:

[42][87][f5]

Now, we simply interpret the decimal equivalent for each part:

  • Red: 4216 = 4(161) + 2(160) = 66
  • Green: 8716 = 8(161) + 7(160) = 135
  • Blue: f516 = 15(161) + 5(160) = 245

That means #4287f5 is really rgb(66, 135, 245)! You can verify this using a Color Converter:

A color converter verifying that #4287f5 is really rgb(66, 135, 245)

For practice, let's convert this to binary as well. I'll mark the groups of four bits to make it easier to see how I did this (note: you can also convert from the decimal RGB representation if you want to):

0x4287f5 = 0b[0100][0010][1000][0111][1111][0101]

Now, two groups of four bits will represent one component of the color (red/green/blue):

0b[01000010][10000111][11110101]

Notice that each color component takes up a byte (8 bits) of space.

How Many Colors Are There?

As an additional exercise, how many unique colors can you possibly have in the modern RGB format?

We know that each component (red/green/blue) is represented using one byte (8 bits). So the colors we're used to are really 24-bit colors.

That means there are a whopping 224 = 16,777,216 possible unique colors that you can generate using hex/rgb! The 24-bit color system is known simply as truecolor. And as you can see, it's capable of representing millions of colors.

Note that you could just as well have performed this calculation using hex: #4287f5. There are six slots, each capable of taking on a value from 0 to f. That gives us a total of 16 × 16 × ... × 16 = 166 = 16,777,216 values—the same result as before.

Or, if you're using the decimal RGB format, the math still pans out: 256 × 256 × 256 = 16,777,216.

What Are 8-Bit Colors?

On older systems with limited memory, colors were represented using just eight bits (one byte). These 8-bit colors had a very limited palette, which meant that most computer graphics didn't have gradual color transitions (so images looked very pixelated/grainy). With only 8 bits to work with, you are limited to just 28 = 256 colors!

An 8-bit color palette

Naturally, you may be wondering: How did they split 8 bits evenly among red, green, and blue? After all, 8 isn't divisible by three!

Well, the answer is that they didn't. The process of splitting these bits among the color components is called color quantization, and the most common method (known as 8-bit truecolor) split the bits as 3-3-2 red-green-blue. Apparently, this is because the human eye is less sensitive to blue light than the other two, and thus it simply made sense to distribute the bits heavily in favor of red and green and leave blue with one less bit to work with.

Signed Binary Number System: Two's Complement

Now that we've covered decimal, binary, and hexadecimal, I'd like us to revisit the binary number system and learn how to represent negative numbers. Because so far, we've only looked at positive numbers. How do we store the negative sign?

To give us some context, I'll assume that we're working with standard 32-bit integers that most (all?) modern computers support. We could just as well look at 64-bit or N-bit integers, but it's good to have a concrete basis for a discussion.

If we have 32 bits to fiddle with, that means we can represent a total of 232 = 4,294,967,296 (4 billion) numbers. More generally, if you have N bits to work with, you can represent 2N values. But we'd like to split this number range evenly between negatives and positives.

Positive or negative... positive or negative. One thing or another thing—ring a bell? That sounds like it's binary in nature. And hey—we're already using binary to store our numbers! Why not reserve just a single bit to represent the sign? We can have the most significant (leading) bit be a 0 when our number is positive and a 1 when it's negative!

Note: This is once again one of those situations where you could just as well do the opposite, except you'd have to convince the whole world to follow your chosen convention.

Earlier, when we were first looking at the binary number systems, I mentioned that you can strip leading zeros because they are meaningless. This is true except when you actually care about distinguishing between positive and negative numbers in binary. Now, we need to be careful—if you strip all leading zeros, you my be left with a leading 1, and that would imply that your number is negative (in a signed number system).

You can think of two's complement as a new perspective or lens through which we look at binary numbers. The number 1002 ordinarily means 410 if we don't care about its sign (i.e., we assume it's unsigned). But if we do care, then we have to ask ourselves (or whoever provided us this number) whether it's a signed number.

How Does Two's Complement Work?

What does a leading 1 actually represent when you expand a signed binary number, and how do we convert a positive number to a negative one, and vice versa?

For example, suppose we're looking at the number 2210, which is represented like this in unsigned binary:

101102

Since we're looking at signed binary, we need to pad this number with an extra 0 out in front (or else a leading 1 would imply that it's negative):

0101102

Okay, so this is positive 2210. How do we represent -2210 in binary?

There are two ways we can do this: the intuitive (longer) approach and the "shortcut" approach. I'll show you both, but I'll start with the more intuitive one.

The Intuitive Approach: What Does a Leading 1 Denote?

Given an N-bit binary string, a leading 1 in two's complement represents -1 multiplied by its corresponding power of two (2N-1). A digit of 1 in any other slot represents +1 times its corresponding power of two.

For example, the signed number 110102 has this expansion:

110102 = -1(24) + 1(23) + 1(21) = -1610 + 810 + 210 = -610

We simply treat the leading 1 as a negative, and that changes the resulting sum in our expansion.

Two's Complement Shortcut: Flip the Bits and Add a 1

To convert a number represented in two's complement binary to its opposite sign, follow these two simple steps:

  1. Flip all of the bits (0 becomes 1 and vice versa).
  2. Add 1 to the result.

For example, let's convert 4310 to -4310 in binary:

+43 in binary: 0101011
Flipped:       1010100
Add one:       1010101
Enter fullscreen mode Exit fullscreen mode

What is this number? It should be -4310, so let's expand it by hand to verify:

-1(26) + 1(24) + 1(22) + 1(20) = -6410 + 1610 + 410 + 110 = -43

Sure enough, the process works!

How Many Signed Binary Numbers Are There?

We've seen that in a signed binary system, the most significant bit is reserved for the sign. What does this do to our number range? Effectively, it halves it!

Let's consider 32-bit integers to give us a concrete basis for discussion. Whereas before we had 32 bits to work with for the magnitude of an unsigned number, we now have only 31 for the magnitude of a signed number:

Unsigned magnitude bits:  [31 30 29 ... 0]
Signed magnitude bits:    31 [30 29 ... 0]
Enter fullscreen mode Exit fullscreen mode

We went from having 232 numbers to 231 positive and negative numbers, which is precisely half of what we started with!

More generally, if you have an N-bit signed binary string, there are going to be 2N values, split evenly between 2N-1 positives and 2N-1 negatives.

Notice that the number zero gets bunched in with the positives and not the negatives:

Signed zero:  0  0  0  0 ... 0 0 0 0
Bits:        31 30 29 28 ... 3 2 1 0
Enter fullscreen mode Exit fullscreen mode

As we're about to see, this has an interesting consequence.

What Is the Largest Signed 32-bit Integer?

The largest signed 32-bit integer is positive, meaning its leading bit is a zero. So we just need to maximize the remaining bits to get the largest possible value:

Num:      0  1  1  1 ... 1
Bits:    31 30 29 28 ... 0
Enter fullscreen mode Exit fullscreen mode

This is 231 - 1, which is 2,147,483,647. In Java, this number is stored in Integer.MAX_VALUE, and in C++, it's std::numeric_limits::max().

More generally, for an N-bit system, the largest signed integer is 2N-1-1.

Why did we subtract a one at the end? Because as I mentioned in the previous section, the number zero gets grouped along with the positives when we split our number range:

Signed zero:  0  0  0  0 ... 0 0 0 0
Bits:        31 30 29 28 ... 3 2 1 0
Enter fullscreen mode Exit fullscreen mode

So to get our largest signed integer, we need to subtract one—we've effectively "lost" a magnitude of one.

Real-World Application: Video Game Currency

In video games like RuneScape that use 32-bit signed integers to represent in-game currency, the max "cash stack" that you can have caps out at exactly 231 - 1, which is roughly 2.1 billion.

The max cash stack you can have in Runescape is 2147m, or 2.1 billion.

Image source: YouTube

Now you know why! If you're wondering why they don't just use unsigned ints, it's because RuneScape runs on Java, and Java doesn't support unsigned ints (except in SE 8+).

What Is the Smallest Signed 32-bit Integer?

This occurs when we set the leading bit to be a 1 and set all remaining bits to be a 0:

Num:      1  0  0  0 ... 0
Bits:    31 30 29 28 ... 0
Enter fullscreen mode Exit fullscreen mode

Why? Because recall that in the expansion of negative numbers in two's complement binary, the leading 1 is a -1 times 2N-1, and a 1 in any other position will be treated as +1 times its corresponding power of two. Since we want the smallest negative number, we don't want any positive terms, as those take away from our magnitude. So we set all remaining bits to be 0.

Answer: -231

In Java, this value is stored in Integer.MIN_VALUE.

In C++, it's in std::numeric_limits::min().

Generalizing things once again, if we have an N-bit system, the smallest representable signed int is -2N-1.

Basic Arithmetic in the Binary Number System

Spoiler: Adding, subtracting, multiplying, and dividing numbers in the binary number system is exactly the same as it is in decimal!

Adding Binary Numbers

We'll first revisit what we learned in elementary school for decimal numbers and then look at how to add two binary numbers.

To add two numbers in the decimal number system, you stack them on top of one another visually and work your way from right to left, adding two digits and "carrying the one" as needed.

Now you should know what carrying the one really means: When you run out of digits to represent something in your fixed-base number system (e.g., 13 isn't a digit in base 10), you represent the part that you can in the current digits place and move over to the next power of your base (the "column" to the left of your current one).

For example, let's add 24 and 18 in decimal:

  24
+ 18
————
  42
Enter fullscreen mode Exit fullscreen mode

We first add the 4 and 8 to get 12, which is not a digit we support in the decimal number system. So we represent the part that we can (2) and carry the remaining value (ten) over to the next column as a 1 (1 × 101 = 1010). There, we have 1 + 2 + 1 = 4:

      1  <-- carried
      24
    + 18
————————
      42
Enter fullscreen mode Exit fullscreen mode

Now, let's add these same two numbers (2410 and 1810) using the binary number system:

  11000
+ 10010
———————
 101010
Enter fullscreen mode Exit fullscreen mode

We work from right to left:

  • Ones place: 0 + 0 = 0
  • Twos place: 0 + 1 = 1
  • Fours place: 0 + 0 = 0
  • Eights place: 1 + 0 = 1
  • Sixteens place: 1 + 1 = 102 (two)

That last step deserves some clarification: When we try to add the two ones, we get 12 + 12 = 102 (two), so we put a 0 in the current column and carry over the 1 to the next power of two, where we have a bunch of implicit leading zeros:

               1      <-- carry bits
0000  ...    00011000
0000  ...  + 00010010
—————————————————————
0000  ...    00101010
Enter fullscreen mode Exit fullscreen mode

In that column, 1 (carried) + 0(implicit) = 1.

If we expand the result, we'll find that it's the same answer we got over in decimal:

1(25) + 1(23) + 1(21) = 32 + 8 + 2 = 4210

Let's look at one more example to get comfortable with carrying bits in binary addition: 2210 + 1410, which we know to be 3610:

  10110
+ 01110
———————
 100100
Enter fullscreen mode Exit fullscreen mode

Something interesting happens when we look at the twos place (the 21 column): We add 12 to 12, giving us two (102), so we put a zero in the 21 column and carry the remaining one.

Now we have three ones in the 22 column: 12(carried) + 12(operand1) + 12(operand2) = 112 (three). So we put a one in the 22 column and carry a one yet again. Rinse and repeat!

               1111    <-- carry bits
0000  ...    00010110
0000  ...  + 00001110
—————————————————————
0000  ...    00100100
Enter fullscreen mode Exit fullscreen mode

Once again, it's a good idea to expand the result so you can verify your work:

1(25) + 1(22) = 3210 + 410 = 3610

Note: We've only looked at examples of adding two binary numbers, but you could just as well stack x numbers on top of one another and add them in binary, just like you would in decimal. How far ahead you need to carry your ones depends on the result that you get in a particular column, represented as a binary string.

Subtracting Binary Numbers

Subtraction is addition with a negative operand: a - b = a + (-b). Now that we know how to represent negative numbers in the binary system thanks to two's complement, this should be a piece of cake: negate the second operand and perform addition.

For example, what's 1210 - 2610? In decimal, we know this to be -1410. Over in binary, we know that 1210 is 01100. What about -2610? We'll represent that using two's complement.

We start by first representing 2610 in binary:

+2610 = 0110102

Now we negate it by flipping the bits and adding one:

26 in binary: 011010
Flipped:      100101
Add one:      100110  = -26
Enter fullscreen mode Exit fullscreen mode

Stack up your operands and add them like we did before:

     11    <-- carry bits
    001100
  + 100110
——————————
    110010
Enter fullscreen mode Exit fullscreen mode

Notice that the result has a leading one, which we know denotes a negative number in signed binary. So we at least got the sign part right! Let's check the magnitude:

-1(25) + 1(24) + 1(21) = -3210 + 1610 + 210 = -1410

See what I mean? Adding and subtracting numbers in the binary number system is just as easy as it is over in decimal.

Multiplying Binary Numbers

Let's remind ourselves how we multiply numbers in decimal:

  21
x 12
————
Enter fullscreen mode Exit fullscreen mode

Remember the process? We multiply the 2 by each digit in the first multiplicand and write out the result under the bar:

  21
x 12
————
  42
Enter fullscreen mode Exit fullscreen mode

Then we move on to the 1 in 12 and repeat the process, but adding a 0 in the right column of the result. Add the two intermediate products to get the answer:

   21
x  12
—————
   42
+ 210
—————
  252
Enter fullscreen mode Exit fullscreen mode

Guess what? The process is exactly the same in the binary number system!

Let's multiply these same two numbers in binary. They are 2110 = 010101 and 1210 = 01100:

   010101
x   01100
—————————
Enter fullscreen mode Exit fullscreen mode

Obviously, this is going to be more involved in binary since we're working with bits (and thus longer strings), but the logic is still the same. In fact, beyond having to write out so many intermediate results, we actually have it much easier over in binary. Whenever a digit is 1, you simply copy down the first multiplicand, padded with zeros. Whenever it's a zero times the first multiplicand, the result is zero!

      010101
x      01100
————————————
      000000
     0000000
    01010100
   010101000
+ 0000000000
————————————
  0011111100
Enter fullscreen mode Exit fullscreen mode

Expanding this in binary, we get:

00111111002 = 1(27) + 1(26) + 1(25) + 1(24) + 1(23) + 1(22) = 25210

Easy peasy. The same process applies regardless of whether your multiplicands are signed or unsigned.

Dividing Binary Numbers

Let's divide 12610 by 1210 using long division:

    0 1 0 . 5
   _______
12 |1 2 6
  - 1 2
   ————
      0 6
    -   0
   ——————
        6 0
      - 6 0
      —————
          0
Enter fullscreen mode Exit fullscreen mode

Answer: 10.5.

Now let's repeat the process over in the binary number system. Note that I'm going to strip leading zeros to make my life easier since we're working with two unsigned numbers:


      _______
1100 |1111110
Enter fullscreen mode Exit fullscreen mode

Take things one digit at a time, and reference this useful YouTube video if you get stuck:

         0 0 0 1 0 1 0 . 1
        ______________
1 1 0 0 |1 1 1 1 1 1 0 . 0
        -0
        ——
         1 1
        -  0
        ————
         1 1 1
        -    0
        ——————
         1 1 1 1
       - 1 1 0 0
        ————————
             1 1 1
          -      0
        ——————————
             1 1 1 1
           - 1 1 0 0
           —————————
             0 0 1 1 0
             -       0
             —————————
                 1 1 0
                 -   0
                 —————
                 1 1 0 0
              -  1 1 0 0
                 ———————
                 0 0 0 0
Enter fullscreen mode Exit fullscreen mode

Answer: 01010.1.

What does the 1 to the right of the decimal point represent? Well, in the decimal number system, anything to the right of the decimal point represents a negative power of ten: 10-1, 10-2, and so on.

As you may have guessed, in the binary number system, these are 2-1, 2-2, and so on. So .1 above really means 1(2-1), which is 1 / 2 = 0.510 in decimal. And of course, the part in front of the decimal point evaluates to 1010.

That gives us 1010 + 0.510 = 10.5. So our answer using binary long division is exactly the same as the one we got over in decimal!

Integer Overflow and Underflow in Binary

What happens if you try to add one to the largest representable N-bit signed integer?

For example, if N = 32, we're really asking what happens if we try adding one to the largest representable 32-bit signed int.

Let's give it a shot:

    0111...11111
  + 0000...00001
————————————————
Enter fullscreen mode Exit fullscreen mode

In the rightmost column, we'll get 12 + 12 = 102, so that's a zero carry a one. But as a result, all of the remaining additions will be 12 + 12 since we'll always carry a one until we get to the leading bit:

    11111111111  <-- carry bits
    0111...11111     (2^{N-1} - 1)
  + 0000...00001     (1)
————————————————
    1000...00000     (-2^{N-1})
Enter fullscreen mode Exit fullscreen mode

And what number is that in signed binary? Hmm... Looks like it's the smallest representable negative number! What we've observed here is called integer overflow. When you try to go past the largest representable signed integer in a given N-bit system, the result overflows or wraps around.

What if we try to subtract one from the smallest representable N-bit signed integer? First, we'll represent -110 as a signed integer in binary:

1 in binary: 0000...00001
Flipped:     1111...11110
Add one:     1111...11111  <-- -1
Enter fullscreen mode Exit fullscreen mode

Now let's add this to the smallest representable signed integer:

   1             <-- carry bits
    1000...00000     (-2^{N-1})
  + 1111...11111     (-1)
————————————————
  1|0111...11111     (2^{N-1} - 1)
Enter fullscreen mode Exit fullscreen mode

Notice that the result carries an additional bit over, yielding a result that has N+1 bits. But our system only supports N bits, so that leading 1 is actually discarded. The result is the largest representable N-bit signed integer, and this is known as integer underflow.

Overflow and underflow are things you should be mindful of in programs that are performing lots of computations, as you may end up getting unexpected results.

The Binary Number System: Additional Topics for Exploration

That about does it for this introduction to the binary number system! We took a pretty in-depth look at decimal, binary, and hexadecimal, and I hope you now have a greater appreciation for the binary number system and the role that it plays in computing.

In reality, there's much more to learn beyond what we covered here. If you're curious, I encourage you to look into representing floating point numbers in binary using the IEE754 format.

I hope you found this helpful! If you struggled with anything in particular, please let me know and I'll try my best to help you out.

Top comments (1)

Collapse
 
teacherbuknoy profile image
Francis Rubio

This is quite exhaustive, wow. Thank you for this.