Representation of Numbers

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:

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: . (Input restrictions).

Note: any spaces in the input are ignored; any other characters that are not 0 or 1 are interpreted as 1, the number is truncated to 8 bits if necessary, and is increased to 8 bits if necessary (by adding zeros on the left).

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

Try 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: . (Input Restrictions) 

For the binary number spaces are ignored, 16 bits are assumed, and any character that is not zero is iunbterpreted as a one. The resulting 16 bit number is displayed with spaces between 4 bit groups, to make the hex interpretation easier to decipher.
For the hex number spaces are ignored, 4 digits are assumed, and any invalid characters are deleted.

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:

Exercises (binary types):

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

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.

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: . (Input Restrictions) 

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



The number must be between 0 and 255. Any non-decimal digits will be replaced by 0.

Signed Integers (i.e., the 2's complement representation)

Represented both positive and negative numbers

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.

3 bit number representation

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:

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

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.

Aside: the diagram below shows the circular representation for 4 bit numbers. To test you knowledge, make sure that you can make equivalent assertions to those listed above for 3 bits (which can represent the numbers −4→3) for the case when there are 4 bits (−8→7).

4 bit number representation


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: . (Input restrictions).

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?

Note: any spaces in the input are ignored; any other characters that are not 0 or 1 are interpreted as 1, the number is truncated to 4 bits if necessary, and is increased to 4 bits if necessary (by adding zeros on the left).

Try these things: You may want to 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: . (Input restrictions).

Note: any spaces in the input are ignored; any other characters that are not 0 or 1 are interpreted as 1, the number is truncated to 8 bits if necessary, and is increased to 8 bits if necessary (by adding zeros on the left).

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)

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.

3 bits to 4, unsigned

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

4 bits to 8, unsigned

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.

3 bits to 4, signed

This can be understood using the image below.

3 bits to 4, signed explained

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

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

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.

4 bits to 8, signed

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.

4 bit, −3

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. . (Input Restrictions) 

The number must be between -128 and 127. Any non-decimal digits will be replaced by 0.

 

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.

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:

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), (Input Restrictions) 

Any spaces in the input are ignored; any other characters that are not 0 or 1 are interpreted as 1, the number is truncated to 8 bits if necessary, and is increased to 8 bits if necessary (by adding zeros on the left). Only one decimal point is allowe.

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.

Observations:

Try this:

Exercises (positive binary fractional numbers):

Convert 37 to binary, shift it right by one and convert back to decimal.  What is the result
Convert the 8 bit Q7 binary number 0.100 1001 from to decimal 
Convert the 8 bit Q7 binary number 0.111 1111 from to decimal
Convert 0.75 from decimal to an 8 bit Q7 binary fraction 
Convert 0.65625 from decimal to an 8 bit Q7 binary fraction 
Approximate 0.9 as an 8 bit Q7 binary fraction

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
Convert 1.111 1111 from binary to decimal
Convert -0.75 from decimal to a binary fraction 
Convert -0.65625 from decimal to a binary fraction 
Approximate -0.9 as a binary fraction (use 8 bits) 

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).

Comments or Questions?

Erik Cheever
Professor Emeritus
Engineering Department
Swarthmore College