Understanding binary numbers is important. All things digital involve circuits and systems in which there are only two possible states. Two voltage levels represent these states; a HIGH and a LOW. In other words, digital devices communicate this way. For a quick review on what it means to be digital, check out the post Analog vs Digital: What’s the Difference?
Binary information that digital systems work with appear as waveforms that represent a sequence of bits. As an example, see the figure below which relates the 1’s and 0’s to voltage levels on the waveform A. As we can see, a 1 is a HIGH and a 0 is a LOW.
Figure 1: a physical representation of what binary numbers mean in a digital waveform.
Being able to count in binary (at least zero through fifteen) and translate from binary to decimal and from decimal to binary is a rite of passage for any serious electronics geek.
There are other number systems like hexadecimal and octal which will undoubtedly show up in other posts.
This one focuses on the basics of binary numbers. If you’re new to digital electronics, it’s a must read.
If you’re not so new it’s a good review.
Let’s take a byte out of binary!
The Decimal Number System – a Quick Review
In order to better understand binary (and other number systems) a few comments on the number system we’re most familiar with are in order.
Counting in binary is just like counting in decimal, if you’re all thumbs.
Let me explain.
The decimal number system (or base 10 system) is based on ten digits 0-9.
We use 10 for our usual number system because we happen to have 10 fingers.
The fact is, any integer greater than 1 can serve as a base for a number system. Somewhere in this universe there may dwell intelligent creatures with twelve fingers that are counting in base 12.
In elementary school, you learned the meaning of decimal (base 10) notation – to interpret a string of digits as a number, you mentally multiply each digit by its place value.
For example, the number 6,053 can also be written as:
6,053 = (6 x 1,000) + (0 x 100) + (5 x 10) + (3 x 1)
Or we can write 6,053 = 6 x 103 + 0 x 102 + 5 x 101 + 3 x 100
Remember that any number, even zero, raised to the zeroth power is one. This is important.
Looking at these stupid-simple equations, which should make you “duh, I already know this,” we see that the 6 in 6,053 occupies the thousands place, the zero occupies the hundreds place, and so on.
Notice also, that because numbers “need to know their place” zero is just as important as any other digit. So it is in binary also.
As a side note, the word decimal comes from the Latin root deci, which means ten.
So, we now know that decimal (or base 10) notation expresses a number as a string of digits in which each digit’s position indicates the power of 10 by which it is multiplied. Truthfully, we’ve known this for a while but probably never explicitly thought of it that way.
Hungry for Binary Numbers
Binary comes from the Latin word bi which means two.
In binary, there are only ones and zeros. A single one or zero is a called a bit, which is a contraction from the words binary and digit.
Four bits make up a nibble and eight bits make up a byte. Usually, you’ll be working with bits and bytes but nibbles may appear here and there.
Finally, a word is usually 2 bytes or sixteen bits but with newer processors can be 32 or even 64 bits.
Given that all the other references have to do with eating, I wonder why they chose the word word. Perhaps they could have called 16 or more bits a munch?
Anyway, the table below summarizes this.
|# OF BITS OR BINARY DIGITS||NAME|
|16+||Word, Double Word (32),
Quad Word (64)
Table 1: common names for groups of binary digits.
Now, consider the binary number 1010, which is 10 in decimal.
The left most bit is the most significant bit or MSB. The right most bit is the least significant bit or LSB. More on this in a bit (pun not totally intended but I couldn’t help myself).
Converting Binary Numbers to Decimal Numbers
The easiest way to convert a binary number to a decimal number or vice versa is to use a calculator.
But real geeks don’t need no stinkin’ calculator, at least to translate four bit numbers back and forth.
You should know all the binary number from 0000 to 1111 (0 to 15).
Converting long binary numbers like 11100001111101010100111 (I have no idea what number this is) into a decimal number without a calculator can be tough. Generally, I use a calculator for anything 5 bits or more.
Remember that like the decimal system, the binary number system is a positional notation system.
So, for any four-bit number we have the positions of:
23 22 21 20
If we consider the binary number 1101, we have one 8, one 4, zero 2s, and one 1.
1 1 0 1
See how the ones and zeros line up with the powers of two? We have a 1 in the 23 position, a 1 in the 22 position, a 0 in the 21 position, and a 1 in the 20 position.
Figure 2 below clarifies this even further.
Figure 2: binary number 13
Once we translate each bit by noting it’s place value and its numerical value, we simply add the numbers together (just like you mentally already do in base 10):
8 + 4 + 0 + 1 = 13
Figure 3 shows all the binary number from zero to fifteen with bit A being the MSB and bit D being the LSB.
Figure 3: binary numbers 0-15.
There are a few things we can note about binary to decimal conversion from figure 3.
First, it is common practice to write each number as a 4-bit number and pad the beginning with zeros (up to 7). This developed because each bit has a physical location in a digital circuit, such as an I/O pin on a microprocessor.
Also note that because each bit holds a position, zero is just as important as one.
Finally, notice that the LSB follows a pattern. The bits alternate with every line making the pattern 0, 1, 0, 1 and so on. The second LSB (bit C) follows a similar pattern of 0, 0, 1, 1, 0, 0… and the third LSB (bit B) makes the pattern 0, 0, 0, 0, 1, 1, 1, 1 then it repeats.
The 2’s bit alternates every 2 lines, the 4’s bit every 4 lines. This pattern can be extended to any number of bits with the number of lines between alterations doubling with each bit to the left.
These patterns can help the noob quickly translate 4 bit binary numbers into decimal numbers.
In general, with n bits, you can count to a number equal to 2n – 1.
Why not 2n?
Because 0 is a number and takes up one of the slots.
Converting Decimal Numbers to Binary Numbers
There are two ways to convert decimal numbers to binary: sum of powers of 2 and repeated division by 2. Let’s talk about both of them.
Sum of Powers of 2
One can convert a decimal number to binary by adding up the powers of 2 by inspection, adding bits as you need them to fill out the total value.
For example, let’s convert 53 to binary using this method.
We first need to ask ourselves what’s the highest power of 2 that is less than 53?
26 is 64 so that wouldn’t work, but we see that 25 is 32.
64 > 53 > 32
So, we set the 32’s bit to 1 and subtract 32 from 53, as shown.
53 – 32 = 21
The largest power of 2 that is less than 21 is 24.
32 > 21 > 16
Again, we set the 16’s bit to 1 and subtract 16 from 21.
21 – 16 = 5
Here we see that 23 is 8 which is greater than the remaining total, so we set the 8’s bit to 0.
The largest power of 2 that is less than 5 is 22.
Set the 4’s bit to one and subtract.
5 – 4 = 1
Like before, we see that 21 is 2 which is greater than 1, so we set that bit to zero. Don’t forget about the zeros!
One is left, so we’ll set the 1’s bit to 1.
Since 1 – 1 = 0, there is nothing left to subtract and we are done.
5310 = 1101012
In the beginning, it may be helpful to write boxes underneath each power of 2 then fill them in with either a 1 or a 0. The image below from one of my text books demonstrates this.
Figure 4: sum of powers of 2 method to convert 92 to binary. From Digital Design with CPLD Applications and VHDL, 2nd Ed.
In the figure above, we start with 64 (26) because 128 (27) is bigger than 92. We then go to each decreasing power of 2 and either put a 1 or a 0 in the box.
For example, in the second iteration we place a 0 in the 32 box because 32 is greater than 28. We then write a 1 in the 16 box because 16 is smaller than 28.
Figure 5 shows a table of various powers of 2 which may be helpful.
Figure 5: powers of 2 table.
Repeated division by 2
Any decimal integer divided by 2 will leave a remainder of either 1 or 0. Thus, repeated division by 2 will leave a string of 1 and 0 remainders. These remainders are the binary equivalent of the decimal number.
The first remainder is the LSB and the last remainder is the MSB.
As an example, suppose we want to convert 43 to binary.
43 / 2 = 21, remainder 1 (LSB).
We’ll keep the remainder and make it the LSB and divide the quotient by 2. This algorithm is repeated until the quotient is 0. The last remainder will be the MSB.
21 / 2 = 10, remainder 1
10 / 2 = 5, remainder 0
5 / 2 = 2, remainder 1
2 / 2 = 1, remainder 0
1 / 2 = 0, remainder 1 (MSB)
Now all we need to do is read the remainders from the bottom up.
So, 4310 = 101011
The figure below, from Digital Fundamentals, 7th Edition, summarizes this process well while converting the number 19 to binary.
Figure 6: converting 19 to binary using repeated division by 2.
Fractional Binary Numbers
Converting fractional decimal numbers to fractional binary numbers can be a little trickier than working with whole numbers.
In decimal, we call the period or dot such as the one in 3.14 a decimal point. In binary, this dot is a binary point. Generally, a period in any number system is called a radix point.
When working in base 10, fractional numbers use the same digits as whole numbers, but we write them to the right of the decimal point. The multipliers for these digits are negative powers of ten such as 10-1, 10-2 and so on rather than positive powers as they were to the left of the decimal point.
Remember that writing any number n-x is the same as writing 1/nx. So, 10-1 is the same as 1/10 or one-tenth.
So it is in the binary system also.
The first four multipliers on either side of the binary point are:
23 22 21 20 . 2-1 2-2 2-3 2-4
=8 =4 =2 =1 = 1/2 =1/4 =1/8 =1/16
As another example, let’s write 0.101101 as a decimal fraction.
1 x 1/2 =1/2
0 x 1/4 = 0
1 x 1/8 = 1/8
1 x 1/16 = 1/16
0 x 1/32 = 0
1 x 1-64 = 1/64
If we add all the fractions together, we get 0.703125.
Simple decimal fractions like 0.5 and 0.25 can be converted to binary fractions by the sum of powers of 2 method.
These particular decimal numbers are the same as ½ and ¼.
0.5 = 0.12 and 0.25 = 0.012.
You might have been able to get these answers just by inspection.
The conversion process becomes more complicated if we convert decimal fractions that cannot be broken into powers of 2. For example, 1/5 (which is 0.2) cannot be exactly represented by a sum of negative powers of 2.
Generally, this is one limitation of computers. Computer scientists, engineers, and programmers have to deal with it sometimes. It will probably come up in future posts about number representation in computers and microcontrollers.
Anyway, for this type of number we need to use the method of repeated multiplication by 2.
The steps below outline the method.
- Multiply the decimal fraction by 2 and note the integer part. This will be either a 0 or a 1 for any number between 0 and 0.9999… This integer part is the first digit to the right of the binary point. Using our example of 0.2 we have: 0.2 x 2 = 0.4, integer part 0.
- Throw out the integer part of the previous product. Multiply the fraction part of the previous product by 2: 0.4 x 2 = 0.8, integer part 0.
- Repeat the above until the fraction either terminates or starts to repeat: 0.8 x 2 = 1.6, integer part 1. We’ll continue our conversion below.
0.6 x 2 = 1.2, integer part 1.
0.2 x 2 = 0.4, integer part 0.
Now we can see that fraction starts to repeat, as the above product is the same as step 1.
Read the integer parts from top to bottom to get the fractional binary number.
0.2 = 0.00110011… which is the same as 0.
The bar shows the portion of the digits that repeat.
Taking a Byte Out of Binary Numbers
So now we know how binary numbers relate to digital circuits and how to convert decimal to binary and vice versa. We also know some lingo for common groupings of bits and something about binary fractions, which is a topic that a lot of other tutorials don’t cover.
Future posts may deal with binary arithmetic and things like 2’s complement and 1’s complement.
Then there’s things like ASCII code, binary coded decimal or BCD, signed binary numbers, floating point, and Gray code to name a few.
The world of electronics has so much to offer.
Until next time,