The difference between a decimal and a binary numeration system.
How computers store data.
What is the difference between 32 bits and 64 bits systems?
Binary
Bits, Bytes
Decimal
Encoding, Encoder
RAM
Let’s define some important words :
\nNumeral : a symbol or a group of characters that represent a number1
A number represent a quantity, something that we can count.
A numeral is a representation of a number.
A numeration system is a set of rules that allow us to count things and to represent quantities.
Every day we manipulate numbers represented with the decimal system: 10.254 , 125, 2020, 31 .…
\nBut the decimal system is not the only one and was not always used.
\nWe can represent numbers using other numeration systems.
\nLet’s take as an example the quantity “one hundred twenty-three”
\n“123” represent this quantity in the decimal system
\n“1111011” represent this quantity in the binary system
\n“7B” represent this quantity in the hexadecimal system
\n“173” represent this quantity in the octal system
\nThe same quantity, the same underlying reality can be expressed in different forms.
\nWhy is it important to understand that? Because computers will not store data using the decimal system. At the physical level, information is stored using zeros and ones.
\nThis chapter will detail how the decimal system and the binary system work.
\n\nDecimal comes from the Latin word “Decimus” which means “tenth”. Whereas binary comes from the Latin “bini” which means “two together”. The etymology of those two words give us a hint about how those systems are constructed :
\nThe binary system uses two symbols which are 0 and 1
The decimal system uses ten symbols which are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
Data written using the binary system will be written using “0” and “1”. For instance, 101010 is written using the binary encoding system.
\nData written using the decimal system will be written using 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. For instance, 42 is written using the decimal system. The number 10 can be encoded using the binary system or the decimal system (and thus, it will not represent the same underlying quantity).
\n\nLet’s take a look at a number written using the decimal system : 123
\nThis number is one hundred twenty-three. This number is composed of “digits”. The first digit represents the number of hundreds. The second digit is the number of tens. The last digit is the number of ones. The decimal system is positional. This means that the contribution of a digit to the number depends on the digit’s position in the number.
\nLet’s take a step further. We can write the number one hundred with the number ten : 100=10\\times10
\nWe can use powers of ten : 10\\times10=10^{2}
\n10^{2} is the equivalent of 10\\times10. We say that 10 is the base and 2 is the the power. When we read it, we say ten raised to the power of 2. Following the same logic, we can write : 10=10^{1}
\nAnd : 1=10^{0}
\nThe last one may seem strange. This is a mathematical rule: any non-zero number raised to 0 is equal to 1.
\nIf you read 1\\times10^{2}+2\\times10^{1}+3\\times10^{0} from left to right, you can note that the power of 10 is decreasing one by one.
\nThis power often correspond to the position of the digit when the number is written down.
\nthe digit “1” is at the position 2 (1\\times10^{2})
the digit “2” is at the position 1 (2\\times10^{1})
the digit “3” is at the position 0 (3\\times10^{0})
The way I counted the positions might be surprising to you. You expected maybe something like that :
\nthe digit “1” is at position 3
the digit “2” is at position 2
the digit “3” is at position 1
This is entirely correct if you start the count with 1, but we start the count with zero. Remember this convention because it will help you later!
\nIn conclusion, the relation between the numeral and the number is the following. :
\nFor a given numeral : \\text{digit}_{2}\\text{digit}_{1}\\text{digit}_{0}
\nThe corresponding number (quantity) is : \\text{digit}_{2}\\times10^{2}+\\text{digit}_{1}\\times10^{1}+\\text{digit}_{0}\\times10^{0}
\nOf course, this relation is valid for numerals with more (and less) than Three digits!
\n\nWe have seen how the system works for round quantities; what about fractional numbers (numbers with a “decimal separator”, like 123.14)
\nThere is still a relationship with the power of tens. 123.45=1\\times10^{2}+2\\times10^{1}+3\\times10^{0}+4\\times\\frac{1}{10^{1}}+5\\times\\frac{1}{10^{2}}
\nThis makes sense because : \\frac{1}{10^{1}}=\\frac{1}{10}=0.1
\n\\frac{1}{10^{2}}=\\frac{1}{100}=0.01
\nA number written in binary is composed of zeros and ones. The binary system, like the decimal system, is a positional numeral system. It means that each digit has a value that depends on its position. It’s a base two system (the decimal system is base 10).
\nLet’s take a look at a binary number : 10_{binary}
\nNote that I added the subscript “binary” to the number because this number also exists in the decimal system. The decimal number 10 is NOT equal to the binary number 10. It does not express the same quantity. 10_{binary} is a numeral composed of two binary digits. The term binary digit has a widely used abbreviation: bit. We can get the decimal equivalent of this number by using powers of two : 10_{binary}=(1\\times2^{1}+0\\times2^{0})_{decimal}
\n10_{binary}=(1\\times2)_{decimal}
\n10_{binary}=2_{decimal}
\nThe binary number 10_{binary} represents the same quantity than 2_{decimal}. It is two units. Let’s take another example. 100010_{binary}
\nWe will find its decimal representation. We take each digit; we multiply it by 2^{x} where x is the position of the digit in the binary number (see figure 2)
\nBy the way, there is a famous joke that I heard once from a colleague about this: “There are ten types of people in the world, the ones that understand binary and the others” :)
\n\nWhat is the maximum decimal number we can store in a two binary digit number? Here is the list of the binary numbers that are composed of 2 digits : 00_{binary}=(0\\times2^{1}+0\\times2^{0})_{decimal}=0_{decimal}
\n01_{binary}=(0\\times2^{1}+1\\times2^{0})_{decimal}=1_{decimal}
\n10_{binary}=(1\\times2^{1}+0\\times2^{0})_{decimal}=2_{decimal}
\n11_{binary}=(1\\times2^{1}+1\\times2^{0})_{decimal}=3_{decimal} With two binary digits, we can store the numbers 0_{decimal} to 3_{decimal}. The maximum number that can be stored is 3_{decimal}
\n\nWhat is the maximum decimal number we can store in a three binary digit number? Here is the list of the binary numbers that are composed of 2 digits : 000_{binary}=(0\\times2^{2}+0\\times2^{1}+0\\times2^{0})_{decimal}=0_{decimal}
\n001_{binary}=(0\\times2^{2}+0\\times2^{1}+1\\times2^{0})_{decimal}=1_{decimal}
\n010_{binary}=(0\\times2^{2}+1\\times2^{1}+0\\times2^{0})_{decimal}=2_{decimal}
\n011_{binary}=(0\\times2^{2}+1\\times2^{1}+1\\times2^{0})_{decimal}=3_{decimal}
\n100_{binary}=(1\\times2^{2}+0\\times2^{1}+0\\times2^{0})_{decimal}=4_{decimal}
\n101_{binary}=(1\\times2^{2}+0\\times2^{1}+1\\times2^{0})_{decimal}=5_{decimal}
\n110_{binary}=(1\\times2^{2}+1\\times2^{1}+0\\times2^{0})_{decimal}=6_{decimal}
\n111_{binary}=(1\\times2^{2}+1\\times2^{1}+1\\times2^{0})_{decimal}=7_{decimal} We can store all the numbers between 0_{decimal} and 7_{decimal} and the maximum decimal number that can be stored on 3 bits is 7_{decimal}.
\n\nWhat is the maximum decimal number we can store in an eight binary digit number? We could list all the different binary numbers that can be built, but it would be time-consuming. Did you notice in the previous sections that the maximum number is made with only ones? It seems logical when a binary number is composed of just ones; its decimal value will be equal to the sum of powers of two (from 0 to n) where n is the number of digits minus 1. If there is a single 0 digit in the binary number, the power of two corresponding is not counted. 11111111_{binary}=(1\\times2^{7}+1\\times2^{6}\\times1\\times2^{5}+1\\times2^{4}+1\\times2^{3}+1\\times2^{2}+1\\times2^{1}+1\\times2^{0})_{decimal}
\n=(1\\times2^{7}+1\\times2^{6}\\times1\\times2^{5}+1\\times2^{4}+1\\times2^{3}+1\\times2^{2}+1\\times2^{1}+1\\times2^{0})_{decimal}
\n=255_{decimal}
\nWith 8 bits, we can store all the numbers between 0_{decimal} and 255_{decimal}.8 bits is called a byte. From 0_{decimal} to 255_{decimal} there are 256 numbers.
\n\nWe detailed how to transform a decimal number into a binary number to store it in memory. I hope it was clear and exciting. It’s therefore not sufficient. Programs manipulate far more things than numbers.
\nWe can build programs that use :
\nImages
Texts
Movies
3D models
.….
How are those objects stored in memory?
\nThe answer is simple: at the end, even photos and movies will be stored using zeros and ones! We will convert them into binary. This job will be performed by specialized programs called encoders. An encoder will take as input a file from a specific format and will convert it into a destination format. In our case, the destination is binary.
\nWe do not need to write those programs; they are all provided by Go. You need to understand that every file or chunk of data is stored under the hood using binary digits (bits).
\nThe binary representation is just hidden from us.
\n\nThe processor of a computer is responsible for executing programs. Most programs need to store and access memory. If you write a program that will display “42” on the screen. Behind the scene, the number 42 needs to be stored somewhere. The system will also need to fetch it from memory.
\nThis is done via an addressing system. Each bit of information is stored in a precise location in the memory unit And to get them, the processor needs to have the ability to get its full address.
\nRemember that there are two types of memory :
\nThe central memory: ROM and RAM
The auxiliary memory: Hard drive, USB keys...
Here we consider only the central memory.
\n\nFor a processor an address is a number. A memory address is like a postal address. It identifies precisely a point in memory space.
\nProcessors will store addresses on registers. A register is a place inside the processor where an address can be saved for later use. For instance, let say that we have a program that makes an addition. Our program defines a variable that will hold the first value (let say 1.234) and another one that will store the second value (let say 1.290.999). When the processor executes our program, it will need to fetch the value 1.234 from memory and the value 1.290.999. The processor will save the two addresses on its registers.
\n\nRegisters have a limited capacity; they can store addresses of a specific size (written in bits). For a 16 bit processor, the maximum capacity of those registers is... 16 bits. For 32 bits processors, the maximum capacity is 32 bits. The same reasoning applies to 64 bits.
\nThe maximum register capacity will define how much memory we can address.
\nWhy? Remember that we store addresses on those registers and an address is a number:
\nOn 32 bits we can store numbers from 0 to 4.294.967.295\\approx2^{32}.
\nOn 64 bits we can store numbers from 0 to 18.446.744.073.709.551.615(\\approx2^{64}).
\nRAM is a hardware component composed of memory cells. Bits are stored in cells. In general, RAM is said to be byte-addressable. It means that the system can fetch data 8 bits at a time.
\nWe have seen that the size of a memory address is limited by the size of registers.
\nA 32-bit system can only handle addresses that are composed of 32 bits. Each bit is composed either of a 0 or a 1, which makes 2^{32}\\approx4 billion possibilities.
\nWith 4 billion addresses, how many bytes can we address?
\n=> 4 billion bytes !
We can convert this quantity into Gigabytes.
\n1 Gigabyte = 1 billion bytes
So, 4 billion bytes = 4 Gigabytes
With a 32 bits systems, 4 Gigabytes of memory can be accessed by the system. Installing more than 4 GB of RAM on a 32 bits system is useless.
\nOn 64 bits systems, the number of addresses is much larger; hence the amount of memory addressable is bigger: theoretically 16 Exabytes (1 Exabyte =1.073.741.824 Gigabytes).
\nWhat is a bit?
What is a byte?
What is an encoder?
What is the maximum amount of RAM that can be addressable by a 32 bit System?
What is the decimal version of (001)_{binary}?
What is a bit?
\nThis is the abbreviation of Binary Digit.
It’s either a 0 or a 1
What is a byte?
\nWhat is an encoder?
\nAn encoder takes input in a given format and transforms it into another form.
The process is reversible.
What is the maximum amount of RAM that can be addressable by a 32 bit System?
\nWhat is the decimal version of (001)_{binary}?
\nNumerals represent numbers
Each numeration system has its way to represent numbers
\nAt the physical level, data is stored in memory using binary digits (0 and 1)
A bit is one binary digit
A byte is eight bits
A 64-bit system can address far more memory than a 32-bit system.
https://www.encyclopedia.com/social-sciences-and-law/economics-business-and-labor/businesses-and-occupations/numeration-systems↩︎
Previous
\n\t\t\t\t\t\t\t\t\tFirst Go Application
\n\t\t\t\t\t\t\t\tNext
\n\t\t\t\t\t\t\t\t\tHexadecimal, octal, ASCII, UTF8, Unicode, Runes
\n\t\t\t\t\t\t\t\t