You can also learn how to translate binary to decimal and back using hexadecimal as a stepping stone between the two number forms.
Using hexadecimal reduces the number of calculations needed to do a bit to byte conversion.
A bit is a fundamental number which can have a value of one or zero. It is a binary number.
Computer technology uses voltage to represent binary using voltage levels since it is easy for digital logic to detect a high or low voltage.
On its own a bit is not much use but combine several bits and use a little maths and you have a useful way of representing numbers.
Binary numbers are represented positionally in the same way that normal decimal numbers are represented. Each bit in a byte represents a higher value depending on its position within the byte.
The difference between a bit and a byte is simply that a byte is made up of several bits (there are 8 bits in a byte - in the past a byte could be 5, 6 or even 9 bits long).
You already know but is is useful to understand the construction of a decimal number so that you can understand binary numbers.
Here's an example: for the number 8362 (decimal - normal).
The digit '2' represents the number of 1's2 * 1
The digit '6' represents the number of 10's6 * 10
The digit '3' represents the number of 100's3 * 100
The digit '8' represents the number of 1000's8 * 1000
Adding all the above up gets back to the original number:
2 + 60 + 300 + 8000 = 8360
As you move to the left the position tells you what multiplier to use for the digit and for decimal it is in powers of 10...
1stdigit from right10 ^ 01
2nddigit from right10 ^ 110
3rddigit from right10 ^ 2100
4thdigit from right10 ^ 31000
The base (or radix) of the decimal system is 10 meaning that there are 10 different symbols used to describe any decimal number:
0 1 2 3 4 5 6 7 8 9
This concept is used to let a computer do mathematics by using the same positional system but using binary, as this is the only language a computer understands!
A byte is simply a group of eight bits with the position of each bit determining its contribution to the total value.
Bytes usually have 8 bits with the highest value the left and the lowest value to the right in the same way as a normal decimal number.
Each bit in a byte is normally referred to using its index number:
Bit position | bit 7 | bit 6 | bit 5 | bit 4 | bit 3 | bit 2 | bit 1 | bit 0 |
Index | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
Bit 0 is is the lowest value bit (or LSB - Least Significant Bit) and bit 7 is the highest value bit (or MSB - Most Significant Bit).
Bit to byte conversion: for converting a binary number of 8 bits into a byte,
Here's an example using the binary number 10001101
Again the number is represented by position - as you move to the left, the multiplier is increased by a power but this time it is a power of two as the base of the number is 2.
1stdigit from right multiplier2 ^ 0= 1
2nddigit from right multiplier2 ^ 1= 2
3rddigit from right multiplier2 ^ 2= 4
4thdigit from right multiplier2 ^ 3= 8
5thdigit from right multiplier2 ^ 4= 16
6thdigit from right multiplier2 ^ 5= 32
7thdigit from right multiplier2 ^ 6= 64
8thdigit from right multiplier2 ^ 7= 128
So the number is :
1stdigit represents the number of 1's1 * 1
2nddigit represents the number of 2's0 * 2
3rddigit represents the number of 4's1 * 4
4thdigit represents the number of 8's1 * 8
5thdigit represents the number of 16's0 * 16
6thdigit represents the number of 32's0 * 32
7thdigit represents the number of 64's0 * 64
8thdigit represents the number of 128's1 * 128
Adding up all the results gives the number (in decimal)
(128*1) + (64*0) + (32*0)+ (16*0)+ (8*1) + (4*1) + (2*0) + (1*1) =
128 + 8 + 4 + 1 = 141
You can scale up the method for as many binary digits as you need.
So, you can change the 8 bit into a byte fairly easily but see later for a simpler method that uses three calculations not eight.
For each bit position you need to figure out if the bit adds to the total value (has a bit value of 1) or does not add to the total (has a bit value of zero).
Starting with the highest bit position value you divide the number by the bit position value. If the result is greater or equal to 1 then it contributes. Here's an example:
If you think of the number 174.
174 / 128= 1.359 : whole part 1- Subtract 127 from 174 leaving 46
46 / 64= 0.718 : whole part 0
46 / 32= 1.438 : whole part 1- Subtract 32 fro 46 leaving 14
14 / 16= 0.875 : whole part 0
14 / 8= 1.750 : whole part 1- subtract 8 from 14 leaving 6
6 / 4= 1.500 : whole part 1- subtract 4 from 6 leaving 2
2 / 2= 1.000 : whole part 1- subtract 2 from 2 leaving 0
0 / 1= 0.000 : whole part 0
So the binary number is 10101110 : you have converted the binary number from a bit into a byte using 16 calculations! (16 if you work as a computer would i.e. do a subtraction for each line regardless of whether the whole number is zero or not).
Again you can scale up the number to as many binary digits as you need.
See later for a simpler method that uses three calculations not 16 for the same bit to byte conversion.
All of that division, multiplication and subtraction is much too much work - imagine having to convert numbers that are 16 bits long 32, even 64 bits long.
Of course easiest way is to use a software calculator program or a calculator that lets you work in hexadecimal or binary and let it do the conversion.
But what if you don't have a calculator (with hex/binary function) or software handy then the following method lets you use an ordinary calculator or even a piece of paper! to do the calculation.
Note you can find a convenient bit to byte (two nibbles) conversion table here.
This method of bit to byte conversion steps around the binary calculation by using hexadecimal as an intermediate step between decimal and binary - you don't need to do a calculation for each bit - it is basically divide and conquer method and you'll probably learn something about number representation as well.
Taking the example in a previous section : 10001101 binary
Bit to byte conversion of a binary number
Binary Number | 10001101 |
Split into nibbles | 1000-1101 |
Convert to hex | 8D |
Process | 8*16+13 = 141 |
So instead of doing 8 calculations you do three and you can do these quickly on paper or with a standard calculator.
Basically, to do a bit to byte conversion, you take an 8 bit binary number and form it into groups of 4 bits (nibbles). You then translate each nibble into a hexadecimal number (a 2 hex digit byte) using this table. You then multiply the left digit by 16 and add the result to the first digit.
Hexadecimal is used as it is simple to translate from binary and is convenient when writing it down especially for longer binary numbers.
Note see later for converting larger numbers
This is the opposite of the previous bit to byte conversion.
Taking the example in a previous section : 174 decimal
Divide by 16 | 174/16=10.875 |
Whole part = left digit | 10 (A in hex) |
Subtract 16*10 from 174 | Leaves 14 (E in hex) |
Convert to binary | AE = 10101110 |
So instead of doing 16 calculations you do three and you can do these quickly on paper or with a standard calculator.
Basically you take the number, divide it by 16 and use the whole part as the left digit and use this table to translate it to a hex character (or see the table later on). Subtract 16 times the whole part from the original number giving you the right digit. Translate this to a hex character. Now translate both hex values to binary using the table and you have the final binary result.
Note see later for converting larger numbers
To easily convert binary numbers to decimal you need to use an intermediate number form - hexadecimal. conversion in the intermediate step is done in using hexadecimal. This is a number having base 16 and is closely related to binary as it shares some of the same power values.
binary position value | hexadecimal | |
1st | digit 2 ^ 0 = 1 | 1st hex digit 16 ^ 0 = 1 |
2nd | digit 2 ^ 1 = 2 | - |
3rd | digit 2 ^ 2 = 4 | - |
4th | digit 2 ^ 3 = 8 | - |
5th | digit 2 ^ 4 = 16 | 2nd hex digit 16 ^ 1 = 16 |
6th | digit 2 ^ 5 = 32 | - |
7th | digit 2 ^ 6 = 64 | - |
8th | digit 2 ^ 7 = 128 | - |
You can see that there are 2 hex digits for every 8 binary digits. This immediately says that using hexadecimal reduces the number of digits (and therefore the number of calculations needed i.e. its simpler).
Base 16 (or hexadecimal) uses 16 digits to represent a number - in the same way that base 10 (decimal) uses ten digits and base 2 (binary) uses two digits. So what symbols do you use for hexadecimal - well you need six more digits.
I never thought where the name came from before now hex = six ! (I have been using hexadecimal for years)
You could use any symbols you want but the conventional system is to use:
A B C D E F (or lower case - doesn't matter )
So what are the hexadecimal values
Decimal | Hex digit | Binary code | Decimal | Hex digit | Binary code |
0 | 0 | 0000 | 8 | 8 | 1000 |
1 | 1 | 0001 | 9 | 9 | 1001 |
2 | 2 | 0010 | 10 | A | 1010 |
3 | 3 | 0011 | 11 | B | 1011 |
4 | 4 | 0100 | 12 | C | 1100 |
5 | 5 | 0101 | 13 | D | 1101 |
6 | 6 | 0110 | 14 | E | 1110 |
7 | 7 | 0111 | 15 | F | 1111 |
This is basically the same method but scaled up with a simple twist.
All you do is split the 16 bit value into two 8 bit values (with the same calculations as for the 8 bit values). The left value is scaled by 256 and that's it.
An example...
Note the range of 16 bit values is 0-65535 or 0 to (2^16-1) minus one as zero is considered a number.
You could probably scale this up to 32 bits or 64 bits but it's probably time better spent if you buy a calculator/software that can do these calculations.
For the binary value 1111010101101101 binary
This process is nearly the same as the 8 bit conversion but just uses two bit to byte conversions.
Binary Number | 1111010101101101 |
Split into nibbles | 1111-0101-0110-1101 |
Convert to hex | F56D |
Split left,right | F5-6D |
Convert each as before | 15*16+5=245 |
and | 6*16+13=109 |
Scale left digit + right | 245*256+109 |
Result | 62829 |
For the decimal value 61049
This process is the opposite of the bit to byte conversion and uses two byte to bit conversion.
Binary Number | 61049 |
Divide by 256 | 61049/256=238.472 |
Left digit is the whole part | 238 |
Subtract 238*256 from original | 121 |
left digit | 238 |
right digit | 121 |
Convert as before | |
238/16 = 14.975 | whole = 14 = E (base 16) |
238 - 14*16 = 14.0 | remainder = 14 = E (base 16) |
121/16 = 7.563 | whole = 7 = 7 (base 16) |
121 - 7*16 =9.0 | remainder 9 = 9 (base 16) |
Full hex number is | EE79 |
substitute binary | 1110-1110-0111-1001 |
or | 1110111001111001 |
Claim Your: Useful
"Arduino Software Guide" Right Now... |
Back to the home page from the bit to byte page.
Eye tracking : How to detect the gaze of your eye and track where you are looking.
How to Easily Use the DS18B20 Maxim One-Wire thermometer with Arduino example code using both external powered and parasitic powered devices.
Do you want to learn how to use a microcontroller in your electronic projects or do you need inspiration for your next project? If so you have found the right place!
New! Comments
Have your say about what you just read! Leave me a comment in the box below.