Binary for Programmers

Let us run through a basic guide for programmers

I’ve seen many presumptions and assumptions that programmers not only know Binary, but can easily convert betweenDenary and Binary and even perform some Binary mathematical operations.

This Medium post represents the missing guide that we need to help us not only understand Binary andDenary, but be in a position to build upon that knowledge when we implement Binary operations in our chosen programming language.

Image for post
Image for post

Difficulty: Beginner | Easy | Normal | Challenging

Prerequisites:

  • None, but for practical purposes it would be useful to be able to produce a “Hello, World!” application in your chosen programming language

Terminology

Base number system: A term that means roughly the same as “Number bases”

Binary: The name of the base 2 number system

Decimal: The system for denoting Integer and Non-Integer numbers

Denary: The name of the base 10 numbering system

Hundred: The number equivalent to the product of ten and ten; ten more than ninety; 100

Integer: A number that has no fractional part, that is no digits after the decimal point

Number bases: The number of different digits that a system of counting uses

Radix: An alternative name for number base

Ten: Equivalent to the product of five and two; one more than nine; 10

Unit: An individual thing, the smallest number

Binary Cheat Sheet: The answer

Binary numbers have a pattern, from Denary numbers to Binary numbers. For comparison I’ve made a rather natty table showing Denary and Binary numbers (with Denary and Binary on the right). Here are the first ten in the table:

Image for post
Image for post

There are minor complexities here. Binary only has 0 and 1 (as we will see later) so 10 in Binary is not ten but when we say the Denary equivalent we would term this to be two.

Whoa, whoa. What does any of this mean?

Ok, let us take this from first principles.

Computers are not human. They run on electricity, that means they have to use electricity to represent information.

It is easier and better to use switches in the computer to store that information. How do we do that?

Why do we count in ten: The mobile example

Here is the iPhone lock screen, and this is how you usually unlock your phone using a passcode

When thinking about this, let us think through a few pertinent questions while thinking of the design.

Image for post
Image for post
  • How many fingers do you have?
  • How many numbers are there on the iPhone screen?

These aren’t difficult questions. There are 10 of each.

We are going to see that 10 is a very important number to the human race, and even has a special name as a numbering system.

This numbering system is our “normal” one for counting and that. We call this Denary and will explain this in the following section:

Denary part 1: 0–9

Our common numbering system is Denary. The system has Units from 0–9 as demonstrated in the following:

Image for post
Image for post

We will return to Denary later to make clear a few principles to do with counting. Let us first make this even easier by explaining Binary which only allows us to use the numbers 0 and 1. Yes, really — there is a numbering system which only allows allows two numbers.

Let us try to explain this with real life examples.

Binary in Real life: The light switch

Think of a light switch

Image for post
Image for post

On a computer the computer saves the information as a single number.

1 = There is power

0 = There is no power

or, to put it another way

We can see all of the options that can be stored on a single switch:

Image for post
Image for post

This could be thought as a numbering system that stores two numbers: zero and one.

The light switch numbering system — One light switch

Largest number that can be stored: 1

Smallest number that can be stored: 0

Total number of options that can be stored: 2

But what if we put more than one light switch in a row (or series?)

Image for post
Image for post
Switch 1 | Switch 2

The light switch numbering system — Two light switches

Largest number that can be stored: 3

Smallest number that can be stored: 0

Toal number of options that can be stored: 4

To demonstrate this, we will go through all of the options for each switch:

Image for post
Image for post

Logically we can continue this process. What is the largest number that three switches can show?

I’ll give you the answer after a break about how counting works for Binary (seriously)

Denary part 2: How to count

When you first start to count you are taught “Denary” — our “normal” counting system.

A history lesson

Denary begins with the letters Dec. Decimal, Decade, December and other words beginning with DEC do so as they actually are related to the Latin word decem (meaning ten). In other words (on some level) Dec = Ten!

A counting lesson

The following language is often used with young children when they learn to count.

The Digits 0–9 are called Units , numbers between 10–90 areTens and numbers between 100–900 are Hundreds.

For example 572 has fiveHundreds, sevenTens and two Units.

Exploring a larger number

Let us take 195, 728, 381. A large number. It contain threeHundreds, eightTens and one Units. We have names for the larger numbers too —

Image for post
Image for post

You can see that you multiply by ten every time a new number is added.

We can see a table of how increasing denary numbers maps to fiveHundreds, Tens and Units.

Image for post
Image for post

We increase the Units each time in order to count. When the Units reach 10, we increase the Tens by one. It then follows that when Tens reach 10, we increase the Hundreds by one.

The column on the left shows the total.

Denary part 3: Rounding up

Denary is the counting system we use for most things in our lives. When we go to the shop, we might buy 15 things including 6 bottles of water.

Denary involves the numbers: 0 to 9

Despite the fact only use those numbers we can create much larger numbers out of them, there seems in fact to be no limit!

Some say we use Denary because we have ten fingers. Others say it is because it is so easy to use. In any case, it seems to have been adopted for most uses in our modern lives.

But what if other numbering systems existed (Hint- they do, and we use them every day).

We are going to explore other numbering systems, that doesn’t use Denary at all!

Binary: An alternative numbering system

Simple Binary: How to count

There is a numbering system that only uses 0 and 1's.

Image for post
Image for post

note that we multiply by two each time a new number is added. If this looks like it is familiar, this is because it is the same for the Denary equivalent that is shown in a section above.

In order to calculate 1110’s equivalent in Denary we perform the following calculation: 8 + 4 + 2 + 0

The reason? It is explained by the following diagram, when we add the result of each Binary place together:

Image for post
Image for post

We can show the possible numbers that can be created with four Binary digits with the following table:

Image for post
Image for post

Essentially we are performing addition to create the Denary equivalent of a Binary number.

Image for post
Image for post

Binary Addition

In Binary there are four different rules (only!) for addition. Addition (+) works much like in Denary as we will see:

0 + 0 = 00 + 1 = 1 1 + 0 = 11 + 1 = 10

The only real difficulty for people is the last one.1 + 1 = 10 makes sense because all of the numbers are their Binary representations.

Image for post
Image for post

That is representation of the following

1 + 1 = 10

because the numbers are Binary representations. Yes, and remember 10 isn’t ten in the world of Binary — it is two!

So we can make this clear by using a subscript to show the numbers are in Base two/ Binary

1₂ + 1₂ = 10₂
Image for post
Image for post

Binary Multiplication

In Binary there are four rules. Now these rules are precisely the rules that apply to Denary numbers. Since Binary only has two numbers (zero and one) the solution to each rule shown is the same in Binary as it is in Denary:

0₂ * 0₂ = 0₂0₂ * 1₂ = 0₂1₂ * 0₂ = 0₂1₂ * 1₂ = 1₂

A simple example is shown in the following:

Image for post
Image for post

Conclusion:

We have covered the basics of addition, multiplication and counting using Binary numbers. The rules for addition, multiplication are even the same for Denary and Binary.

The Twitter contact:

Any questions? You can get in touch with me here

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store