What is the minimum number of bits needed to represent 6 things?

This is an substantially updated version of an older file that can be found here.

The sections below labeled "*interactive*" contain modules that can let you explore scenarios of your own choosing.

Contents

When working with any kind of digital system (electronics or computers), it is important to understand the different ways in which these numbers are represented. Almost without exception, numbers are represented by two voltage levels which can represent a one or a zero (an interesting exception to this rule are newer memory devices that use one four (or more) possible voltage levels, thereby increasing the amount of information that can be stored by a single memory cell). The number system based on ones and zeroes is called the binary system (because there are only two possible digits). Before discussing the binary system, a review of the decimal (ten possible digits) system is in order because many of the concepts of the binary system will be easier to understand when introduced alongside their decimal counterpart. The "base" of the system is also called the "radix".

Finding the Decimal Equivalent of the Number with a Different Radix (e.g., binary→decimal)

Positive Decimal Integers

You are familiar with the decimal system. For instance, to represent the positive integer one hundred and twenty-five as a decimal number, we can write (with the postivie sign implied). The subscript 10 denotes the number as a base 10 (decimal) number. After the first line, all numbers are implicitly base 10.

Some things to note (that we will be able to apply to the representation of unsigned binary numbers):

Some observations:

  • The rightmost digit (5 in this case) is multiplied by 100, the next digit (3 in this case) to the left is multiplied by 101, and so on. Each successive digit to the left has a multiplier that is 10 times the previous digit.
  • With n digits, 10n unique numbers (from 0 to 10n−1) can be represented. If n=3, 1000 (=103) numbers can be represented 0-999.
  • To see how many digits a number needs, you can simply take the logarithm (base 10) of the absolute value of the number, and add 1 to it. The integer part of the result is the number of digits. For instance, log10(33) + 1 = 2.5; the integer part of that is 2, so 2 digits are needed.
    For this example log10(725) + 1 = 3.86; The integer part of that is 3, so 3 digits are needed.
  • To multiply a number by 10 you can simply shift it to the left by one digit, and fill in the rightmost digit with a 0 (moving the decimal place one to the right). To divide a number by 10, simply shift the number to the right by one digit (moving the decimal place one to the left).
  • Negative numbers are handled easily by simply putting a minus sign (−) in front of the number. This does lead, however, to the somewhat awkward situation where 0=−0. We will avoid this situation with binary representations, but with a little bit of effort.

Finding the decimal equivalent of an unsigned (positive) binary integer (*interactive*)

To represent a number in binary, every digit has to be either 0 or 1 (as opposed to decimal numbers where a digit can take any value from 0 to 9).The subscript 2 denotes a binary (i.e., base 2) number. Each digit in a binary number is called a bit.. Likewise we can make a similar set of observations:

To see how the decimal equivalent of an 8 bit unsigned binary number can be calculated, enter an 8 bit unsigned binary number: . .

Any number can be broken down this way, by finding all of the powers of 2 that add up to the number in question; you can see this is exactly analagous to the decimal deconstruction done earlier

  • The rightmost digit is multiplied by 20, the next digit to the left is multiplied by 21, and so on. Each successive digit to the left has a multiplier that is 2 times the previous digit.
  • With n digits, 2n unique numbers (from 0 to 2n−1) can be represented. If n=8, 256 (=208) numbers can be represented 0−255.
  • To see how many digits a number needs, you can simply take the logarithm (base 2) of the absolute value of the number, and add 1 to it. The integer part of the result is the number of digits. For instance, log10(53) + 1 = 6.7; the integer part of that is 6, so 6 digits are needed.

    Recall that log2(x)=log(x)/log(2) (where log() can be either the natural log, or log base 10).


  • To multiply a number by 2 you can simply shift it to the left by one digit, and fill in the rightmost digit with a 0 (moving the decimal place one to the right). To divide a number by 2, simply shift the number to the right by one digit (moving the decimal place one to the left).
  • At this point we can't represent negative numbers, but will do so soon.

Try this:

  • Convert 0001 0011

    (spaces are ignored but can help make the number easier to read, much like commas in large decimal numbers)

    from binary to decimal (you can check yourself by entering it above). Make sure you understand the result.
  • Enter 0000 0011. Then 0000 0110, then 0000 1100.

    Note that as the binary number is shifted to the left by one, the value of the number doubles.

  • Enter 1111 1111. As expected the result is 255=28−1.
  • Think of a number, and try to find the binary representation. Later in this document we will find a systematic way to do this.

Hexadecimal, Octal, Bits, Bytes and Words (*interactive*).

It is often convenient to handle groups of bits, rather than dealing with theindividually. The most common grouping is 8 bits, which forms a byte. A single byte can represent 256 (28) numbers. Memory capacity is usually referred to in bytes. Two bytes is usually called a word, or short word (though word-length depends on the application). A two-byte word is also the size that is usually used to represent integers in programming languages. A long word is usually twice as long as a word. A less common unit is the nibble which is 4 bits, or half of a byte.

It is cumbersome for humans to deal with writing, reading and remembering the large number bits, and it takes many of them to represent even fairly small numbers. A number of different ways have been developed to make the handling of binary data easier for us. The most common is to use hexadecimal notation. In hexadecimal notation, 4 bits (a nibble) are represented by a single digit. There is obviously a problem with this since 4 bits gives 16 possible combinations, and there are only 10 unique decimal digits, 0 to 9. This is solved by using the first 6 letters (A..F or a..f) of the alphabet as numbers. The table shows the relationship between decimal, hexadecimal and binary.

Decimal Hexadecimal Binary
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

There are some significant advantages to using hexadecimal when dealing with electronic representations of numbers (if people had 16 fingers, we wouldn't be saddled with the awkward decimal system). Using hexadecimal makes it very easy to convert back and forth from binary because each hexadecimal digit corresponds to exactly 4 bits (log 2(16) = 4) and each byte is two hexadecimal digit. In contrast, a decimal digit corresponds to log2(10) = 3.322 bits and a byte is 2.408 decimal digits. Clearly hexadecimal is better suited to the task of representing binary numbers than is decimal.

As an example, the 16 bit number 0CA316 = 0000 1100 1010 00112 (00002 = 016, 11002 = C16 , 10102 = A16, 00112 = 3 16). It is convenient to write the binary number with spaces after every fourth bit to make it easier to read.

Try it yourself: (enter either a value into either the binary or hexadecimal (i.e., hex) text box)
Binary value       Hexadecimal value: .

Converting a hexadecimal number to its decimal equivalent is slightly more difficult, but can be done in the same way as before but multiplying each digit by the appropriate power of 16 (instead of powers of 2 (for binary) or powers of 10 (for decimal)).

Octal notation is yet another compact method for writing binary numbers. There are 8 octal characters, 0...7. Obviously this can be represented by exactly 3 bits. Two octal digits can represent numbers up to 64, and three octal digits up to 512

Summary of binary types:

  • bit: a single binary digit, either zero or one.
  • byte: 8 bits, can represent positive numbers from 0 to 255.
  • hexadecimal: A representation of 4 bits by a single digit 0..9,A..F (or 0..9,a..f). In this way a byte can be represented by two hexadecimal digits.
  • long word: A long word is usually twice as long as a word
  • nibble (or nybble): 4 bits, half of a byte.
  • octal: A representation of 3 bits by a single digit 0..7. This is used much less commonly than it once was, but it is still seen in special circumstances
  • word: Usually 16 bits, or two bytes. But a word can be almost any size, depending on the application being considered -- 32 and 64 bits are other common sizes.

Exercises (binary types):

Convert 3C from hexadecimal to decimal  Answer 60
Convert 1010 0111 1011 from binary to hexadecimal  Answer A7B
Convert 7D0 from hexadecimal to binary  Answer 0111 1101 0000
If you shift a hexadecimal number to the left by one digit, how many times larger is the resulting number?  Answer 16

Converting a Decimal Number to a Different Radix (e.g., decimal→binary);

Positive Decimal Integers

To begin our discussion on converting decimal numbers to a different radix, let's begin with a discussion of how we could find the individual digits of a decimal number, say 73510. Obviously we can just look at the number in this case (the 1's place is 5, the 10's place is thirty and the 100's place is 7), but let's find an algorithmic way of doing this, so a computer can do it. It will also lead us to a technique for converting to binary (or any other radix).

Find the decimal digits that comprise the number 73510.

  • We begin by performing an integer division by 10 (recall that decimal is radix 10).
    735 ÷ 10 = 73 r 5 (this is read as 735 divided by 10 is 73 with a remainer of 5; the quotient is 73 and the remainder is 5). This tells us that the rightmost digit (i.e., the remainder) is 5.
  • Repeat process with quotient: 73 ÷ 10 = 7 r 3. So the next digit is 3.
  • Repeat process with quotient: 7 ÷ 10 = 0 r 7. So the last digit is 7.
  • Since the new quotient is 0, we are done.

  • The three digits found are (left→right) 7, 3, and 5, which is also the list of remainders bottom→top.

Finding the unsigned binary equivalent of a positive decimal integer (*interactive*)

We can now perform conversions from decimal to binary using the same procedure but dividing the number by 2 each time, until the quotient of the division is zero.
Enter a decimal number between 0 and 255: .

The procedure is described verbally on the left, and is shown in a more compact tabular form on the right.

To represent signed integers we will use what is called the 2's complement representation. Let's start by representing 3 bit unsigned integers on a circle, as shown on the left half of the diagram shown below. This is the method we've been using up until now; only positive numbers (and 0) can be represented.

What is the minimum number of bits needed to represent 6 things?

The image on the left shows the decimal number in red, and the equivalent binary number in blue. For example, the number 2dec (on the right side of the circle) is the same as 0102. Note:

  • Since there are n=3 bits, there are 2n=23=8 unique numbers that can be represented.
  • The decimal numbers go from 0 to 2n-1=23−1=7 (i.e., 0→7).
  • To add, we move clockwise around the circle (i.e., to go from 2dec=0102 to 3dec=0112, we move 1 place clockwise; to go from 2dec=0102 to 5dec=1102, we move 3 places). To subtract, we move counterclockwise.
  • We get an error in addition or subtraction if we cross the part of the circle with the dashed magenta line (between 7dec and 0dec). For example if we are a 1dec=0012, and subtract 3, then we move 3 spaces counterclockwise and end up at 6dec=1102, which is clearly incorrect because we crossed the dashed magenta line.

To represent negative numbers in 2's complement notation, we refer to the right half of the diagram. We can make similar observations:

  • Since there are n=3 bits, there are 2n=23=8 unique numbers that can be represented.
  • The decimal numbers go from −(2n−1)=−(22)=−4 to 2n−1−1=22−1=3 (i.e., −4→3).
  • To add, we move clockwise (i.e., to go from 2dec=0102 to 3dec=0112, we move 1 place clockwise; to go from −2dec=1102 to 1dec=0012, we move 3 places). To subtract, we move counterclockwise.
  • We get an error in addition or subtraction if we cross the part of the circle with the dashed magenta line (between 3dec and −4dec). For example if we are a 2dec=0102, and add 3, then we move 3 spaces clockwise and end up at −3dec=1012, which is clearly incorrect.

Note that the binary numbers are identical, it is solely the way that we interpret them that changes. The substantive difference between the two representations is that 2's complement numbers can be either positive or negative as indicated by the leftmost bit. Note that for all of the positive numbers (and 0) the leftmost bit is a zero, and for all of the negative numbers the leftmost bit is a one.

Finding the decimal equivalent of a 2's complement binary integer (*interactive*)

The process for finding the decimal equivalent of a 2's complement binary integer is very similar to the way we do it for unsigned numbers (see above), but we must interpret the leftmost bit differently. For an n bit number, if the leftmost bit is 1 we interpret it as a value of −(2n−1), if it is 0 we ignore it. All the other bits are interpreted as before.

4 bit 2's complement:

For a four bit number −(2n−1)=−(24−1)=−(23−1)=−8.

To check your knowledge of signed numbers, explore 4 bit 2's complement numbers. Enter a 4 bit 2's complement number: . . You can also try entering a 4 bit number here, and then covert it to 8 bits via sign extension, and enter it above.

If the 4 bit binary number 10112=−5dec, how do you represent −5dec with 8 bits?

Try these things: You may want to check the 4 bit number wheel shown previously.

  • Enter the number "0011", and the result is 3. Change the leftmost bit to "1" (so the number is "1011"), and the result is −5 (or −8+3; the −8 is from the (leftmost) sign bit, and the number 3 is from the rightmost 3 bits, or "011").
  • Convert the 2's complement binary number "0111" to decimal. Predict, then check the result if the number is changed to "1111". Where would these two numbers be on the 4 bit 2's complement number wheel?

    Note if any 2's complement binary number is all 1's (i.e., "1111" for four bits, or "1111 1111" for 8 bits) it is always equivalent to the decimal number −1. If you are not sure why, check the number wheels shown previously.

  • Convert (by hand) the number "1101", then check your result Hint (hover the mouse

    here

    for an explanation).
    Where would this number be on the number wheel? If you are not sure, check the 4 bit number wheel shown previously.

8 bit 2's complement

For an 8 bit number this means that if the leftmost bit is set, we interpret it as −128 (=−(28−1)=−(27)).

To see how the decimal equivalent of an 8 bit 2's complement binary number can be calculated, enter an 8 bit 2's complement number: . .

This is almost exactly the way we broke down an unsigned number previously. The only difference is how we treat the leftmost bit. If it is a "1" we add in −(2n−1)·1=−128·1=128; if it is a 0 we add in −(2n−1)·0=−128·0=0. In other words, if the leftmost bit is "0" we proceed exactly as we did with an unsigned number.

Try these things: (Note: I put a space in the middle of the binary numbers to make them easier to read - you can choose to add these (or not) to the input text box)

  • Enter the number "0011 0101", and the result is 53. Change the leftmost bit to "1" (so the number is "1011 0101"), and the result is −75 (or −128+53; the −128 is from the sign bit, and the number 53 is from the remainder of the bits).
  • Convert the 2's complement binary number "0111 1111" to decimal. Predict, then check the result if the number is changed to "1111 1111". Where would these two numbers be on a number wheel?

    Note if any 2's complement binary number is all 1's (i.e., "1111" for four bits, or "1111 1111" for 8 bits) it is always equivalent to the decimal number −1. If you are not sure why, check the number wheels shown previously.

  • Convert (by hand) the number "1000 0101", then check your result Hint (hover the mouse

    here

    for an explanation).
    Where would this number be on a number wheel?

Sign Extension (increasing the number of bits in a 2's complement number)

For an unsigned number, increasing the number of bits used to represent a number can be accomplished by simply adding 0's to the left side of a number. For example, the number 3dec can be represented with 3 bits as 0112, or as 4 bits as 00112.

What is the minimum number of bits needed to represent 6 things?

We can continue this process for 8 bits, or even more, and the number represented doesn't change.

What is the minimum number of bits needed to represent 6 things?

The process above works for positive 2's complement numbers, but not for negative numbers. Recall that for negative numbers that the leftmost bit is a "1." To increase the number of bits, we place an additional "1" to the left of the original number. For example, consider the 3 bit representation of the number −3dec=1012 (the leftmost bit represents −4, and the rightmost bit represents 1, and when added together the result is 3). To increase to 4 bits we add a "1" to the left, which yields 11012=−3dec (see the discussion above for 4 bit 2's complement numbers if this is unclear). This is shown below, with the 3 bit version on the top line, and the 4 bit version below.

What is the minimum number of bits needed to represent 6 things?

This can be understood using the image below.

What is the minimum number of bits needed to represent 6 things?

The top line (101) shows the original 3 bit number.

  • The leftmost bit represents −(2n−1) where n=3, or −(23−1)=−4.
  • The middle bit is zero and doesn't contribute
  • The rightmost bit represents 1.
  • The sum of the 3 bits is −3.

The second line (1101) show the 4 bit representation of the same number.

  • The leftmost bit represents −(2n−1) where n=4, or −(24−1)=−8.
  • The next bit represents 22 or 4.
  • The third bit from the left is zero and doesn't contribute.
  • The rightmost bit represents 1.
  • The sum of the 4 bits is −3.

Note that the sum of the 2 leftmost bits in the 4 bit number is equal to −4, which is the same as the leftmost bit, by itself, in the three bit number — so adding the bit to the left has no effect on the final sum. This means we can keep repeating this process to find the 8 (or more) bit representation.

What is the minimum number of bits needed to represent 6 things?

This process of increasing the number of bits used to represent is called "sign extension" because we simply extend the leftmost (sign) bit to fill in the added bits in the larger number. If the sign bit is "1", all of the new bits will be 1; if the sign bit is zero, all of the new bits will be 0. In the image below the sign bit of the 4 bit numbers is underlined, and you can see that it is simply repeated in the new bits of the 8 bit representation.

As a final exercise, you can look at the 3 bit and 4 bit number wheels shown above, and verify that you can convert 3 bit numbers to their 4 bit equivalent by using sign extension.

Convert from 2's complement binary to decimal (*interactive*).

Converting a 2's complement number from binary to decimal is very similar to converting an unsigned number, but we must account for the sign bit. If the number is positive (i.e., the sign bit is "0") the process is unchanged. If the number is negative, we know the sign bit is 1, and we just need to find the rest of the bits. An example may help to clarify things.

Consider the 4 bit 2's complement number, 11012=−3dec (this is the same number used in the discussion of sign extension). The value of the bits that are set are shown below.

What is the minimum number of bits needed to represent 6 things?

Since the number is negative we know that the sign bit (the leftmost bit) is set to "1". We just need to find the rest of the bits. We see that we can effectively remove the sign bit by adding +8 to the number (+8=2n−1=23, where n is the number of bits in the number — in this case n=4 bits); this eliminates the effect of the sign bit. This process yields a result of 5dec=1012. So the resulting 2's complement number is 11012, where the leftmost bit is the sign bit (i.e., −8), and the other bits represent the number 5 (so the total is −8+5=−3, as desired).

You can try this for yourself by typing in a number that can be represented as an 8 bit 2's complement number (i.e., since the number of bits is n=8, the number must be in the range from −128→+127 (the lower limit is −(2n−1)=−(27)=−128, and the upper limit is 2(n−1)−1=128−1=127).

Enter a number between −127 and 128, and you can see how it is converted to and 8 bit 2's complement number. .

this way, by finding all of the powers of 2 that add up to the number in question; you can see this is exactly analagous to the decimal deconstruction done earlier. Let's look at how this changes the value of some binary numbers

Binary Unsigned Signed
0010 0011 35 35
1010 0011 163 -93
1111 1111 255 -1
1000 0000 128 -128

If Bit 7 is not set (as in the first example) the representation of signed and unsigned numbers is the same. However, when Bit 7 is set, the number is always negative. For this reason Bit 7 is sometimes called the sign bit. Signed numbers are added in the same way as unsigned numbers, the only difference is in the way they are interpreted. This is important for designers of arithmetic circuitry because it means that numbers can be added by the same circuitry regardless of whether or not they are signed.

To form a two's complement number that is negative you simply take the corresponding positive number, invert all the bits, and add 1. The example below illustrated this by forming the number negative 35 as a two's complement integer:

3510 = 0010 00112
invert -> 1101 11002
add 1 -> 1101 11012


So 1101 1101 is our two's complement representation of -35. We can check this by adding up the contributions from the individual bits

1101 11012 = -128 + 64 + 0 + 16 + 8 + 4 + 0 + 1 = -35.

The same procedure (invert and add 1) is used to convert the negative number to its positive equivalent. If we want to know what what number is represented by 1111 1101, we apply the procedure again

? = 1111 11012
invert -> 0000 00102
add 1 -> 0000 00112


Since 0000 0011 represents the number 3, we know that 1111 1101 represents the number -3.

    Exercises (binary integers):

Note that a number can be extended from 4 bits to 8 bits by simply repeating the leftmost bit 4 times. Consider the following examples

Decimal 4 bit 8 bit
3 0011 0000 0011
-3 1101 1111 1101
7 0111 0000 0111
-5 1011 1111 1011

Let's carefully consider the last case which uses the number -5. As a 4 bit number this is represented as

1011 = -8 + 2 + 1 = -5

The 8 bit number is

1111 1011 = -128 + 64 + 32 + 16 + 8 + 2 + 1 = -5.

It is clear that in the second case the sum of the contributions from the leftmost 5 bits (-128 + 64 + 32 + 16 + 8 = -8) is the same as the contribution from the leftmost bit in the 4 bit representation (-8)

This process is refered to as sign-extension, and can be applied whenever a number is to be represented by a larger number of bits. Likewise you can remove all but one of the leftmost bits, as long as they are all the same, so the 8 bit number 000001112=710 can be replaced by 01112. Also 111110112=-510 can be replaced by 10112).

Most processors even have two separate instructions for shifting numbers to the right (which, you will recall, is equivalent to dividing the number in half). The first instruction is something like LSR (Logical Shift Right) which simply shifts the bits to the right and usually fills a zero in as the lefmost bit. The second instruction is something like ASR (Arithmetic Shift Right), which shifts all of the bits to the right, while keeping the leftmost bit unchanged. With ASR 1010 (-6) becomes 1101 (-3). Of course, there is only one instruction for a left shift (since LSL is equivalent to ASL).

Positive non-integer numbers

Note: If you are only interested in integer numbers you can skip the rest of this page. If you are interested in how non-integer numbers can be respresented in binary, keep reading.

Positive non-integer decimal numbers

Representing positive numbers that are not integers is a simple extension of the representation of integers. To review the concepts involved, let's start with an example using decimal numbers then we will continue with binary numbers. We proceed as we did for positive integers, but we include negative powers of ten to the right of the decimal point. To wit,

The only pertinent observations here are:

  • If there are m digits to the right of the decimal point, the smallest number that can be represented is 10-m. For instance if m=4, the smallest number that can be represented is 0.0001=10-4.

Positive binary fractional numbers - Binary→Decimal (*interactive*)

Representing positive numbers that are not integers is a simple extension of binary integers, but we include negative powers of two to the right of the decimal point.
Enter a number (8 bits with a decimal point),

Q Notation: We define a binary number with a fractional part by the number of bits and with Q equal to the number of bits to the right of the decimal point.

  • The number 0011.0101 is an 8 bit, Q4 number.
  • The number 0.1110101 is an 8 bit Q7 number.

Observations:

  • If there are n digits to the left of the decimal point and Q digits to the right of the decimal point (for a total of n+Q bits), the smallest number that can be represented is 2-Q, and the largest is 2n-2-Q.
    For instance if we have an 8 bit Q5 number then n=3,
    • the smallest number that can be represented is 000.000012 = 2-5 = 1/32 = 0.03125 and
    • the largest is 111.11112 = 7.96875 = 23-2-5
    • If this isn't clear, try entering binary numbers (with decimal point) in the text box immediately above.

Try this:

  • Flip the bit immediately to the right of the decimal point. How does the number change?
  • Move the decimal point one to the right or one to the left. What happens?
  • Set the decimal point for an 8 bit Q3 number.
    • What is the smallest number you can represent?
    • What is the largest number?

Exercises (positive binary fractional numbers):

Convert 37 to binary, shift it right by one and convert back to decimal.  What is the result Answer 18.5 (=37/2)
Convert the 8 bit Q7 binary number 0.100 1001 from to decimal  Answer 0.5703125
Convert the 8 bit Q7 binary number 0.111 1111 from to decimal Answer 0.9921875
Convert 0.75 from decimal to an 8 bit Q7 binary fraction  Answer 0.110 0000
Convert 0.65625 from decimal to an 8 bit Q7 binary fraction  Answer 0.101 0100
Approximate 0.9 as an 8 bit Q7 binary fraction Answer 0.111 0011

Signed binary fractions

Signed binary fractions are formed much like signed integers. We will work with a single digit to the left of the decimal point, and this will represent the number -1 (= -(20)). The rest of the representation of the fraction remains unchanged. Therefore this leftmost bit represents a sign bit just as with two's complement integers. If this bit is set, the number is negative, otherwise the number is positive. The largest positive number that can be represented is still 1-2-m but the largest negative number is -1. The resolution is still 1-2-m.

There is a terminology for naming the resolution of signed fractions. If there are m bits to the right of the decimal point, the number is said to be in Qm format. For a 16 bit number (15 bits to the right of the decimal point) this results in Q15 notation.

Exercises (signed binary fractions):

Convert 1.100 1001 from binary to decimal Answer -0.4296875
Convert 1.111 1111 from binary to decimal Answer -0.0078125
Convert -0.75 from decimal to a binary fraction  Answer 1.010 0000
Convert -0.65625 from decimal to a binary fraction  Answer 1.010 1100
Approximate -0.9 as a binary fraction (use 8 bits)  Answer 1.000 1101

Signed binary fractions are easily extended to include all numbers by representing the number to the left of the decimal point as a 2's complement integer, and the number to the right of the decimal point as a positive fraction. Thus

-6.62510 = (-7+0.375)10 = 1001.0112

Note, that as with two's complement integers, the leftmost digit can be repeated any number of times without affecting the value of the number.

A Quicker Method for Converting Binary Fractions.

Another way to convert Qm numbers to decimal is to represent the binary number as a signed integer, and to divide by 2m; this is equivalent to shifting the decimal point m places to the right. To convert a decimal number to Qm, multiply the number by 2m and take the rightmost m digits. Note, this simply truncates the number; it is more elegant, and accurate, but slightly more complicated, to round the number.

Examples (all Q7 numbers):

Convert 0.100 1001 to decimal. Take the binary number 0100 1001 (=7310), and divide by 27=128. The answer is 73/128=0.5703125, which agrees with the result of the previous exercise (Positive Binary Fractions).
Convert 1.100 1001  to decimal. Take the two's complements binary number 1100 1001 (=-5510), and divide by 128. The answer is -0.4296875, which agrees with the result of the previous exercise (Signed Binary Fractions).
Convert 0.9 to Q7 format Multiply 0.9 by 128 to get  115.2. This is represented in binary as 111 0011, so the Q7 representation is 0.111 0011. This agrees with the result of the previous exercise (Positive Binary Fractions).
Convert -0.9 to Q7 format Multiply -0.9 by 128 to get  -115.2. The Q7 representation is 1.000 1101. This agrees with the result of the previous exercise (Signed Binary Fractions).