Lecture 2
Binary and Hexadecimal Numbers
Purpose:
• Review binary and hexadecimal number representations
• Convert directly from one base to another base
• Review addition and subtraction in binary representations
• Determine overflow in unsigned and signed binary addition and
subtraction
The Need for Other Bases
Humans are used to the decimal number system, also called radix-10 or base-10. To state the obvious, base-10 means that a digit has one of ten possible values, 0 through 9. In computers, numbers are stored in binary, also called radix-2 or base-2, using arrays of flip-flops. Each digit may take one of two values, either 0 or 1. Long strings of these 1’s and 0’s are cumbersome to use, so we will usually represent binary numbers using hexadecimal, also called radix-16 or base-16. It is important to note that no computer actually stores values in hardware using hexadecimal. This number system is only a convenience for humans.
All of these number systems are positional.
Unsigned Decimal
Numbers are represented using the digits 0, 1, 2, …, 9. Multi-digit numbers are interpreted as in the following example:
793
10=
Unsigned Binary
Numbers are represented using the digits 0 and 1. Multi-digit numbers are interpreted as in the following example:
10111
2=
In binary, each digit is called a bit. Since we use binary to represent the values stored in a group of flip-flops, we usually specify a binary system by the number of bits (flip-flops) being used to store each number.
When we write numbers in this system, we will write all bits, including
leading 0’s. The number above is expressed in 5-bit binary. The
number below is in 8-bit binary.
Unsigned Hexadecimal
Numbers are represented in hexadecimal using the digits 0, 1, 2, …, 9, A, B, …, F where the letters represent values: A=10, B=11, and so on to F=15. Note that this gives sixteen possible values for each digit. Multi- digit numbers are interpreted as in the following example:
76CA
16=
Notes on Bases
Since all three number bases will be used, including the correct subscript when a number is written out of context is mandatory.
Pronunciation
Words like “ten,” “twenty,” and “one-thousand” refer to specific numbers of items, regardless of how the numbers are written. To avoid confusion, binary and hexadecimal numbers are spoken by naming the digits followed by “binary” or “hexadecimal.” For example,
1000
16is pronounced “one zero zero zero hexadecimal.”
“One-thousand” is actually 3E8
16.
Ranges of Unsigned Number Systems
System Lowest Highest Number
of Values 4-bit binary
(1-digit hex)
8-bit binary (1 byte) (2-digit hex) 16-bit binary
(2 bytes) (4-digit hex)
n-bit binary
2’s Complement Binary Numbers
Most microprocessors today use 2’s Complement numbers to represent systems with positive and negative values. Hardware performs addition and subtraction on binary values the same way whether they represent unsigned systems or 2’s complement systems, and this greatly simplifies the design of the processor
The only difference between unsigned binary systems and 2’s comp.
binary systems is that the most significant bit in signed systems has a weight of -2
n-1. Both systems are also defined by the number of bits being used, and as with unsigned, we must write down all bits. To distinguish between the two, we will use a “2c” subscript to indicate a 2’s comp. number.
Example: Convert 10011010
2cto decimal.
Example: Convert 11011
2cto decimal.
Example: Convert 01011
2cto decimal.
It is very important to note that microprocessors usually view a group
of bits as simply a group of bits. It is the human that interprets the
group as an unsigned value, signed value, or also as just a group of bits.
Ranges of Signed Number Systems
System Least Greatest
Number of Values 4-bit binary
8-bit binary
16-bit binary
n-bit binary
Note that the least representable value has a single 1 in the column with a negative weight and 0’s in the columns with positive weights.
The greatest representable number is just the opposite: 1’s in the
positively weighted columns and a 0 in the column with a negative
weight.
Sign Bit
Since the leftmost column has a negative weight, and the magnitude of that weight is larger than the weights of all the positive columns added together, any number with a 1 in the leftmost column will be negative.
If the leftmost bit has a 0, then there is no negative contribution, and the value will be positive. Hence, the sign of the number can be determined by inspection.
Negating a 2’s Complement Number
The official way to negate a value is to subtract it from zero. This will generate a number with the same magnitude but with the opposite sign. The second method is to perform the 2’s complement, which is the following two steps:
1. Perform the 1’s complement (flip all the bits) 2. Add 1
Example: Negate 00101001
2c(41
10)
Converting Between Number Systems
Given the three number systems (binary, hexadecimal, and decimal), there are six possible conversions to allow us to convert directly from one to another.
Binary to Decimal: This was covered earlier in this lecture by determining the weights for each column and adding them up, either as unsigned or as signed.
Hexadecimal to Decimal: This was also covered earlier in this lecture by determining the weights for each column and adding them up.
Decimal to Binary (or Hexadecimal): This conversion is more of a process than the others.
1. Successively divide the decimal number by the new base and keep track of the remainders generated.
2. Stop dividing once the quotient reaches 0.
3. Write the remainders in the opposite order than they were generated.
4. Add leading digits if necessary.
Example: Convert 53
10to 8-bit unsigned binary.
Example: Convert 172
10to 2-digit hexadecimal.
the final answer.
Binary to Hexadecimal: This conversion is the reason that hexadecimal is used in the first place. Since once hexadecimal digit can represent 16 different values, and four bits can also represent 16 (2
4) different values, the bits of the binary number will be grouped together in 4’s and replaced by the hexadecimal digit with the same value.
Example: Convert the binary numbers below to hexadecimal.
100101011110
2011010101011
2cNote that a binary number may not always contain an integer multiple of 4 bits. In these cases, always extend the binary number by padding it with zeros, whether the number is unsigned or signed.
Example: Convert the binary numbers below to hexadecimal.
Hexadecimal to Binary: As with binary to hexadecimal, this conversion is by inspection. Each hexadecimal digit is replaced with the four bits that represents the same value.
Example: Convert the following hexadecimal numbers to binary.
BEFA
1673FC
16Hexadecimal is not usually interpreted as signed or unsigned. It is simply a more convenient method for humans to discuss binary patterns. By default, we will assume that the binary pattern has four bits for every hexadecimal digits used, as done above. However, we can also specify a binary system with any number of bits, as done on the latter half of the previous slide. We must explicitly state the binary system being used, and we will ignore the padding 0’s.
Example: Convert the following hexadecimal numbers to the specified binary system.
07B
16to 9-bit signed
1F
16to 5-bit unsigned
Binary Arithmetic
Unsigned and signed addition and subtraction generate the same numerical result. The difference is determining if overflow or underflow (usually grouped together generically as “overflow”) occurs. The methods below work for both addition and subtraction.
For unsigned:
For signed:
The above methods are easily implemented in hardware. For humans, there is an alternate approach for signed arithmetic.
Addition: If the two numbers being added have the same sign, the answer must have that sign. Otherwise, overflow occurs. Signed addition cannot generate overflow if the two numbers being added have different signs.
Subtraction:
Example: Determine the result, if unsigned overflow occurred, and if signed overflow occurred. (Note that the binary numbers are shown with an unsigned subscript for simplicity.)
0 1 1 0 1 0 1 1
2+ 0 1 0 1 1 0 1 0
20 1 1 0 0 1 0 1
2- 1 1 0 1 1 0 1 1
2Here’s another way to visualize overflow. Binary systems are modular.
For our purposes, modular means that a constant number of digits are 0000
1000
1100 0100
0001 1111
0010 1110
1101 0011
0101
0110 1010
1011
1001 0111
“Wrap-around”
point for unsigned
“Wrap-around”
point for signed