# Binary.

### A brief history.

Binary as a number system is something that is often overlooked. Getting stuck into coding and programming concepts is way more fun than looking at the machine code that underpins it. However, the history of Binary and the power that it has granted us as programmers, should be shown a BIT of love.

Although you may not think it, Binary has roots granted in many different cultures. It has been found in documents in ancient Egypt as far back as 2400 BC in the form of binary fractions, used in India in the 200 BC and China from the 9th Century. In fact the use of Binary in China in the form of hexagrams was a fascination of Leibniz. He saw the use of hexagrams to be akin to his own theory of philosophical mathematics and affirmation of his religious belief, the creation of something out of nothing. Or as we know it the movement of 0 to 1.

Later on in the 1800’s George Boole formed an algebraic system of logic that became know as Boolean algebra and was pivotal to the design of digital electronic circuitry. It was through his work that other were able to experiment using electrical relays and switches, which later  brought about the development of computing machinery. These were the precursors to modern day computers.

### Binary Vs. Decimal.

Binary is a number system used for mathematics just the same as the decimal system and the hexadecimal system. The standard system for counting relies upon the Arabic numerals (0 – 9) and is called the decimal system because ten characters are used to represent the base. The least significant number takes the rightmost position and is referred to as the first digit. The digit is incremented until the 9 character is reached, when this happens the first digit is set back to 0 and a second digit is used and set to 1. So we go from 9 to 10. Simple.

This may seem trivial but understanding how decimals are represented is important to understand how Binary is represented. You can take any number that is written and split it into it base parts. Take the number 182736 for example, although written it is self explanatory it is represented as follows; Each number is multiplied by 10 to the power of whatever the position from the right is. So the first digit it multiplied by 10⁰, this is the repeated as we move from right to left. Each movement we raise the power by 1.

Okay, great. But what has this got to do with Binary? We can count much in the same way in Binary and any number that we have in the decimal system can be represented in Binary. However, there are two key differences. First of all we only have two characters, 1 and 0, and because of this the base is represented as 2n. We can count in Binary like so; Each digit in Binary is referred to as a bit and we can count much in the same manner as we can in Decimal. Any decimal integer can be converted into binary by dividing it by two. The quotient is then recorded on the right side, and you keep going until you get a quotient of either 1 or 0. Take the number 16;

16 / 2 = 8 + 0

8 / 2 = 4 + 0

4 / 2 = 2 + 0

2 / 2 = 1 + 0

And written down would read 10000.

### Bits and Bytes.

So now that we have a basic understanding of the Binary system, lets move on to what this means for us as programmers. As mentioned above a bit is one instance of either 1 or 0. Other than for mathematics this isn’t really much help to us. Computers encode data through bytes, which is a collections of 8 bits and it is through bytes that we can store one character. If you were to calculate all the possible combinations of a byte we come to the number of 256 (2⁸). You may be more familiar with 255 (RGB values) as computers tend to count from 0 rather than 1. It is through these numbers that we are able to use characters, numbers, symbols and logical operations in our code. This is also what is more commonly known as machine code, as its the stuff which is normally hidden away from us.

Its important to note that the encoding above refers to ASCII (American Standard Code for Information Interchange) and was the basis of older computers. We may still use it in our code without knowing it, but due to the internet and the need for uniformity, most programs use Unicode which typically uses 2 bytes per character to allow for the use of non-alphabet characters, UTF-8 is one of three encodings that is part of Unicode.

### Types.

In computing data types have a machine size. This refers to the typical amount of memory that is allotted for different types of data. The basic data types usually are Integers, Floats, Boolean and Chars(Strings are collections of chars).

• Integers – These are whole numbers and are usually composed of 32 bits or 4 bytes and have a range of +2,147,483,647 to -2,147,483,648. Numbers that are larger are composed of 64 bits or 8 bytes and have a range of +9,223,372,036,854,775,807 to -9,223,372,036,854,775,808. Ruby represents these as Fixnum(32 bits) and Bignum(64 bits).
• Floats – These are inexact ‘real’ numbers and work much in the same way as above. They are usually referred to as have precision(32 bits) and double precision (64 bits). In Ruby all floats have double precision. Floats in practice may look as simple as 0.3 but in reality are more like 0.3000000000000005 and rounded.
• Boolean – These are typically represented by 1 or 0 and only take up 1 bit however their size can vary.
• Chars – These can be any unicode characters and are composed of 16 bits or 2 bytes. Strings are made up of a string of characters (hence then name String).

Whilst in practice its rare to need to truly understand how much memory data types take up and can be frustrating to work out due to language differences, its worth seeing how different data has different sizes.