Bits, bytes, nibbles, binary and hex.
This article will start with the basic definitions of bits, bytes etc, go through how data is stored in bits and bytes, and eventually discuss how the binary and hexadecimal number systems work, how to convert between the two and decimal and how that can be useful. Lays a foundation for later articles...
What is a bit?
A 'bit' is a single bit of information. It is a simple 1 or 0, yes or no, on or off, a binary digit. Everything starts with a bit. A single bit can only store information about whether something is true or false. But combined with more bits they can store bigger numbers. They do this of course through the binary number system (base 2). If you know how to deal with non-decimal number systems then its all quite straightforward. If you don't know how to deal with them I"ll summarize the binary system briefly below. But first...
The Decimal (base10) System
The decimal number system is the number system that we humans use in our everyday lives. Probably derived due to the fact that we have 10 fingers. It simply means that we have 10 digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) in our numbering system, if we want bigger numbers we simply add a new column of digits.
When we see a number like 7586. We automatically deconstruct it as seven thousand, five hundred, eight-y, six.
We know it means 7*1000 + 5*100 + 8*10 + 6.
Or 7*10*10*0 + 5*10*10 + 8*10 + 6.
Or 7*10^3 + 5*10^2 + 8*10^1 + 6*10^0 (note: any number to the power of 0 is 1)
Thats how the number system works. In this case, we simply multiply the number in each column by 10 (because thats how many numbers we have in our system) to the power of which ever column its in minus one.
That last sentence maybe complicated but just remember it as:
7,586 = 7*1000 + 5*100 + 8*10 + 6 = 7*10^3 + 5*10^2 + 8*10^1 + 6*10^0.
The binary (base2) system
A binary number is simply a number (of any length) which has only 2 digits: 0 and 1 (analogous to how in our decimal base10 we have 10 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9).
E.g. 1100100101100010 (can be written with spaces such as 1100 1001 0110 0010 or 11 00 10 01 01 10 00 10 etc. to aid readability).
These mean a lot to electrical devices, but us carbon based life-forms are more used to working in base10 (the decimal system). So how do we convert these to decimal?
Easy, using the same method we used to deconstruct a decimal number.
Bin to Dec
If we see 101100 as a binary number, we can think of it as:
1*2^5 + 0*2^4 + 1*2^3 + 1*2^2 + 0*2^1 + 0 = 1*32 + 0*16 + 1*8 + 1*4 + 0*2 + 0*1 = 44.
Instead of multiplying each column digit with powers of 10, we multiply with powers of 2 (because its base2, see the pattern? :P).
Dec to Bin
Going back is the same but in reverse:
If we want to convert 44 to binary, first we need to find the biggest power of two that is smaller than our number.
In this case 32 (5th power). So we know that the 5th column is 'on' (is 1).
Then we add the next biggest power of 2 (16, 4th column) and see if its bigger.
32 + 16 is 48 and is bigger than 44 so the 4th column must be 'empty' (is 0).
And we carry on.
Next power of 2 is 8 (3rd column). 32 + 8 is 40. It is smaller than 44 so we keep it (3rd column is 1).
Next power of 2 is 4 (2nd column). 40 + 4 is 44. It is equal to our number so we keep it (2nd column is 1).
We don't need to carry on as we've matched the number so the 1st and 0th columns must be 0.
An easier way of doing the same thing is constantly dividing by 2, rounding down and keeping track of the remainder.
44/2 = 22, remainder 0
22/2 = 11, remainder 0
11/2 = 5, remainder 1
5/2 = 2, remainder 1
2/2 = 1, remainder 0
1/2 = 0, remainder 1
and read this bottom upwards = 101100.
When dealing with binary numbers it is useful to know their powers so I've listed some at the bottom (yes I did write them from memory :P)
So what does 8bit or 10bit or 16bit or any-bit mean?
It simply means that 8, or 10, or 16 or any number bits are sitting side by side, and ultimately determines the range of values that can be held by that group of bits. Look at the following logic:
A single bit can have two states: 1 or 0.
If we put 2 bits side by side, how many states can we have? The answer is 4 states. 2*2. And these states are 00, 01, 10, 11.
If we add another bit (so 3 bits in total) to the system how many states can we have? The answer is 8 (2*2*2, or 2^3). And these states are 000, 001, 010, 011, 100, 101, 110, 111.
Lets add yet another bit (so 4 bits in total). The number of states we can have is 16 (2*2*2*2, or 2^4). These states are all of the states in the 3bit version with a zero at the beginning, and also with a 1 at the beginning.
What is a byte? Nibble? etc...
A byte is simply eight contiguous bits (also known as an octet). It is eight 1s or 0s side by side. So it can store 2^8 = 2*2*2*2*2*2*2*2 = 256 values. Ignoring the possibility of negative numbers (more on signed integers later), that means a byte can hold numbers 0 to 255. (P.S. A byte wasn't always 8bits, but I'll skip that to avoid confusion, just google it you're curious).
Half of a byte, is of course a Nibble (sometimes written nybble). It is 4 bits. The important thing to remember is that a 4 bit number means it can store 2^4 = 16 values. Usually 0 to 15 if it is unsigned.
Before we look into how all this is useful, we need to look at one more thing.
The Hexadecimal (base16) System
Now we will introduce the hexadecimal (base16) system. These are the numbers which look like 4E8FH8 etc. (usually preceded by 0x or #). They have 16 digits in their numbering system. Because we only have 10 numerical digits (0-9) available to us in our (latin) alphabet, we use the letters A, B, C, D, E, F assigning them the values of 10,11,12,13,14,15 respectively. (Case does not matter, you can use A or a, same thing).
So if you see hexadecimal E you should know straight away that it's the value 14 (in our normal decimal base10 number system) - I've added a table at the bottom.
Hex to Dec
So how do you know the decimal value of B7E? The same way we deconstruct long numbers in decimal or binary, we do the same in hexadecimal.
B7E = B*16*16 + 7*16 + E = 2942.
Dec to Hex
We do the same thing as in Bin to Dec, except diving by 16 this time.
2942/16 = 183, remainder = 14 => E in hex. (use the mod (modulus) operator to find the remainder. 2942 % 16 in most languages. Or you can do 2942 - 16*183. same thing).
183/16 = 11, remainder = 7.
11/16 = 0, remainder = 11 => B in hex.
So reading from bottom up, our number is B7E.
Hex Notation
Programming languages use different notations to represent hexadecimal numbers. I will use the 0x prefix as used by C and similar languages(C++, Java, JavaScript, Action-Script) etc. (E.g. 0xFF4500).
HTML and CSS use a #prefix (e.g. #FF4500).
These don't affect the value in any way apart from telling the compiler/interpreter that the value is hexadecimal. So if you pass the number 0x27 to a function, its actually passing 27 in base16, which is 2*16 + 7 = 39.
http://en.wikipedia.org/wiki/Hexadecimal#Representing_hexadecimal has more info on the matter.
How is all this useful?
Because everything on computers is stored internally with bits (base2). And hexadecimal being base16, is a power of 2, so converting between bin and hex is much easier than involving decimal.
One hexadecimal digit fills exactly 4 bits (which as we remember, is called a nibble).
So when we see a long binary number such as 1100100101100010, we can space it in groups of 4 bits such as 1100 1001 0110 0010 and tackle each nibble one at a time and quickly write it down as 0xC962.
It also means that when we see a pretty long hex number, e.g. 0x60FD369C, we can easily convert it to binary and/or extract its byte or nibble components.
But how is that useful?
Later posts (such as the one about how color is stored) will reveal the inner beauty of dealing with Binary and Hexadecimal.
Hex - Bin Table
0 0 0000
1 1 0001
2 2 0010
3 3 0011
4 4 0100
5 5 0101
6 6 0110
7 7 0111
8 8 1000
9 9 1001
10 A 1010
11 B 1011
12 C 1100
13 D 1101
14 E 1110
15 F 1111
Some Powers of 2
2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
2^4 = 16
2^5 = 32
2^6 = 64
2^7 = 128
2^8 = 256
2^9 = 512
2^10 = 1,024
2^11 = 2,048
2^12 = 4,096
2^13 = 8,192
2^14 = 16,384
2^15 = 32,768
2^16 = 65,536
2^18 = 262,144
2^20 = 1,048,576
2^24 = 16,777,216
2 ^ 30 = 1,073,741,824
2 ^ 32 = 4,294,967,296
Delicious
Digg
StumbleUpon





