| « The Microcontroller | Truth tables NEVER lie » |
Binary, the language of machine, but why? In circuitry we have two things we can represent data as. We can use a "on" and a "off" meaning, weather there is current flowing or there is not. Traditionally 1 represents an on, or a high and 0 represents an off or a low. Because of this we work in binary, because there are only two numbers in that system 0 and 1.
Number systems
We work with the decimal system which has values 0-9. This was primarily done more than likely because we have 10 digits which we've used over the centuries to count :D (now if we only counted fingers and not digits we'd be perfectly happy with octal). Each digit in a number represents the base to a power starting at 0 and ending at infinity. To put this to perspective lets look at the number 142. When we break 142 apart we get:
1 4 2
1x10^2 + 4X10^1 + 2X10^0
100+40+2
142
This works for all number systems from binary to hexadecimal and beyond.
Binary
Lets look at a binary number, 1001001 and break it apart.
1 0 0 1 0 0 1
1*2^6 + 0*2^5 + 0*2^4 + 1*2^3 + 0*2^2 + 0*2^1 + 1*2^0
64+0+0+8+0+0+1
73
1001001 is a binary representation of the decimal number 73. Binary doesn't look so daunting anymore does it?
Bits and Bytes
If you've ever been around computers or anything digital you've heard the terms 8-bit 32-bit 64-bit 100 Gigabyte etc. One bit represents one digit of a binary number, either a 1 or a 0. A byte represents 8-bits or two hexadecimal digits(well get on hexadecimal later). Bits and bytes are the main ways computers handle data. Bits being fundamental and bytes being a way to organize bits in a way that they're useful. You cant hold that much information with a bit, however with a byte you can hold up to 256 values (2^8)
Hexadecimal
Even though bits are fundamental we are going to be working with hexadecimal. As you can see, to represent large numbers in binary can get rather confusing. We use hexadecimal to bring things down to size and make code and large registers more manageable. Hexadecimal is a number system based on base 16. If you noticed, 16 is a power of two thus every 4 digits in binary represent 1 hexadecimal digit. We note that a number is hexadecimal with the prefix "0x" or the suffix "h." One thing you might be wondering is how do you represent numbers greater than 9 without using two digits? Simple, we use letters. In hexadecimal we use the number 0-9 and the letters A-F (10-15). So the hexadecimal number 0xB is 11 in decimal and 1011 in binary.
Expanding Hexadecimal
So lets do a quick exercise to see how hexadecimal truly works. Lets start with 0x5FB
To convert this to binary we break it down digit by digit starting with the first.
0xB in binary is 1011. The second digit 0xF is 1111 in binary and 0x5 is 0101 in binary.
We put this all together and get 01011111011. Because hexadecimal is based on a number that is a power of 2, we can work with numbers like this.
Expanding it to decimal isn't all that hard either, however a calculator would help for those tricky powers of 16.
0x5FB
5*16^2 + 15*16^1 + 11*16^0
1280 + 240 + 11 = 1531
In reality, with a little work and patience working with other number systems will come natural and become easier the more you work with them. With the fundamentals of electronics, semiconductors and these number systems we can finally start looking at the microcontroller.