0% found this document useful (0 votes)
23 views40 pages

Week 2 Digital Numbers & Operations

This document discusses binary numbers and arithmetic operations. It covers binary and hexadecimal number representations, signed and unsigned integers, fixed point numbers, and basic arithmetic operations like addition and subtraction. Conversion between decimal, binary, and hexadecimal numbering systems is explained.

Uploaded by

8ms46xxt9q
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views40 pages

Week 2 Digital Numbers & Operations

This document discusses binary numbers and arithmetic operations. It covers binary and hexadecimal number representations, signed and unsigned integers, fixed point numbers, and basic arithmetic operations like addition and subtraction. Conversion between decimal, binary, and hexadecimal numbering systems is explained.

Uploaded by

8ms46xxt9q
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

2- Binary Numbers and Arithmetic

Operations

EE1501 – Digital Logic


Frederic Surre

School of Mathematics, Computer Science and Engineering 2016


In this chapter …

 Binary numbers:
• What is a binary number?
• How to convert a decimal number into a binary number.

 Hexadecimal numbers:
• Definition
• How to use hexadecimal number to represent bits

 Representation of Numerical data:


• Signed integers: signed magnitude and 2’s complement
• Real number: fixed point representation

 Arithmetic operations:
• Addition/Subtraction of integer
 Shift/Rotation
 Design issues

2016 EE1501 – Digital Logic 2


1. Binary Numbers

Binary numbers

 Digital systems are restricted to using combinations of 1’s & 0’s to


represent all information.

 How can you communicate information using just one’s and


zero’s ?
• Let’s start with what we know: decimal system.
• Decimal system is used to represent numbers
Example: 135, -24, 3.625

 How a number is represented in decimal system?


• Use of base 10: 135 = 1x102+3x101+5x100
• Coefficient can take any integer value between 0 and 9.

 Can we adapt this decomposition for binary numbers?


2016 EE1501 – Digital Logic 3
1. Binary Numbers

Binary numbers

 Each digit can only take the values ‘0’ or ‘1’


 Therefore, we should work in base 2.
 What does that mean?
Example: 10112=1x23+0x22+1x21+1x20

 Decimal system can not be directly represented with binary


variables but can be deduced using base 2 decomposition
 Let’s look at an example:
Convert 101011 into decimal number

 Obviously words and other non numerical data are not equivalent
to binary variables.
 Remark: To express words with binary variables, another approach
is needed (this is out of the scope of this module)
2016 EE1501 – Digital Logic 4
1. Binary Numbers

Binary numbers: Some definitions

 Each successive digit in a binary number is called a bit


 The Most Significant Bit (MSB) is the extreme left-hand digit,
and is raised to 2n-1 , where n is the total number of bits used
 The Least Significant Bit (LSB) is the extreme right, and it
normally represents 20 = 1
Example:
0 1 1 1 0 0 1 1
MSB LSB

 Usually many bits are used in a digital system.


 Other names are used to describe the use of more than one bit.

2016 EE1501 – Digital Logic 5


1. Binary Numbers

Binary numbers: Some definitions

 4 bits = 1 Nibble can represent 16 different values


 8 bits = 1 Byte can represent 256 values
 16 bits = 1 Word can represent 65,536 values
 32 bits = 1 Double Word can represent 4,294,967,296 values.
 64 bits = 1 Quadruple Word can represent
18,446,744,073,709,551,616 values !
 The number of bits that a digital system can handle at one time is
dependent on the system design.

2016 EE1501 – Digital Logic 6


1. Binary Numbers

Binary numbers: Conversion between bases

Conversion: binary to decimal


Example: Convert 001001112 to decimal

value 27 26 25 24 23 22 21 20
(128) (64) (32) (16) (8) (4) (2) (1)

Binary number 0 0 1 0 0 1 1 1

So we can state:
(0  27 )  (0  26 )  (1  25 )  (0  24 )  (0  23 )  (1  22 )  (1  21 )  (1  20 )

Or more simply
(1  25 )  (1  22 )  (1  21 )  (1  20 )
001001112  39

2016 EE1501 – Digital Logic 7


1. Binary Numbers

Binary numbers: Conversion between bases

First method: Repeated division by 2


Step 1: We start with the decimal number and divide it by 2. The result is a whole number
quotient and a reminder, R0. The reminder can either be equal to 0 or 0.5.
If R0 = 0, then the LSB = 0. If R0 = 0.5, then the LSB = 1.
Step 2: Divide the whole number quotient from step 1 by 2. Keep the whole number quotient
and check the reminder, R1. If R1 = 0, then the bit next to the LSB equals 0; if R1 = 0.5, then this
bit equals 1.
Step 3: Repeat the same process until the whole number quotient equals 0.
Step 4: Add as many 0 as needed to the left of the last bit found in Step 3 to have the number
of bit required. (this step may not be needed if every bit has been used or if the number of bit
is not given)

Example: Convert 100 to binary using the repeated division by 2


method.
Exercise: Represent the decimal value 57 as a binary number
2016 EE1501 – Digital Logic 8
1. Binary Numbers

Binary numbers: Conversion between bases

Second method:

Conversion: decimal to binary


Example: Convert 100 to binary

value 27 26 25 24 23 22 21 20
(128) (64) (32) (16) (8) (4) (2) (1)

Binary number 0 1 1 0 0 1 0 0

The binary equivalent of the decimal value 100 is 011001002

2016 EE1501 – Digital Logic 9


1. Binary Numbers

Binary numbers: How to manipulate binary numbers

 Although the computer has no problem with large binary numbers,


humans do!
 Imagine having to work with 16 bit binary numbers !
Example: 0111011011000111

 For example, now, computers are using 64 bits binary numbers


Example:
0111011011000111010101111100110101110010010101100111010011001111

 To help the design of large digital systems, e.g. computers, a


simplified approach should be used.
 So a short-hand is required to enable humans to easily handle large
binary numbers

2016 EE1501 – Digital Logic 10


2. Hexadecimal Numbers

Hexadecimal numbers

 Hexadecimal = base 16

 Computers can not work with hexadecimal numbers.

 However, working in base 16 has advantages for humans:


• Compact: Each character can represent 16 possible values
• Simple conversion: Easy to convert between hex and binary
• Less error prone than raw binary

2016 EE1501 – Digital Logic 11


2. Hexadecimal Numbers

Hexadecimal numbers

How can we represent 16 values in one digit ?


 Sixteen values can be represented by a 4 bit binary number.

 0 - 9 are represented as in decimal.

 The remaining six values (10-15) are each represented by a letter


A-F

Decimal 10 11 12 13 14 15
Binary 4-bits 1010 1011 1100 1101 1110 1111
Hexadecimal A B C D E F

2016 EE1501 – Digital Logic 12


2. Hexadecimal Numbers

Hexadecimal numbers

 Correspondence hexadecimal - binary

Hexa Binary Hexa Binary


0 0000 8 1000
1 0001 9 1001
2 0010 A 1010
3 0011 B 1011
4 0100 C 1100
5 0101 D 1101
6 0110 E 1110
7 0111 F 1111

2016 EE1501 – Digital Logic 13


2. Hexadecimal Numbers

Hexadecimal numbers

How can a number larger than 15 can be represented in hexadecimal?

 If a number is larger than 15, it will require more than 4 bits to be


represented in binary.

 From the binary representation,


• separate the bits in group of 4 starting from the left (LSB).
• replace each group of four bits by its binary equivalent

 From the decimal representation:


• convert decimal to binary and then to hexadecimal

 It is also possible to convert directly to hexadecimal (using base 16)


but it creates problems for negative numbers. Therefore, you
should not calculate the hexadecimal directly from the decimal
number. Only the method above is used.
2016 EE1501 – Digital Logic 14
2. Hexadecimal Numbers

Hexadecimal numbers

 Examples:
Convert the binary number : 010110111110
- In decimal
- In hexadecimal

Convert the decimal number: 79


- In binary
- In hexadecimal from binary

2016 EE1501 – Digital Logic 15


3. Other Decimal Coding

Binary Coded Decimal (BCD)

 An alternative method to represent numbers in binary

 Like Hexadecimal system, number is broken up and each digit is


represented separately as 4 bits:

 One number between 0 and 9 is coded.


• Quick to code
• Useful format when interfacing to hardware
• Inefficient use of bits
• Difficult to use in arithmetic operations

 4 bits can code 16 numbers. However, only 10 are coded in BCD.


 Other types of code exists and can be find in reference books. We
will study a couple more as examples during the module

2016 EE1501 – Digital Logic 16


4. Numerical Data

Numerical data

 Unsigned integers: whole numbers never have a value less than


zero and can be represented by standard binary number
With k bits, 2k integers can be represented, ranging from 0 to 2k-1

Example: 4 bits can represent integer from 0 to 24-1=15


8 bits can represent integer from 0 to 28-1=255

 Signed integers: whole numbers can have ± values. Two solutions:


• Signed magnitude
• (1’s Complement)
• 2’s Complement

2016 EE1501 – Digital Logic 17


4. Numerical Data

Signed magnitude

 MSB used to indicate sign of decimal number

 MSB has no numerical value and is called the sign bit

 The remaining bits are used to express the magnitude of the


decimal number

 Example 1: Express the values 45 and –45 as signed magnitude


binary numbers.
45 = 00101101
–45 = 10101101

 This is a simple and easy solution. Let’s look in more details


into it.

2016 EE1501 – Digital Logic 18


4. Numerical Data

Signed magnitude

 Example 2: Perform the following operations to the binary


numbers obtained in the previous example:
• Add 1 to both numbers
• Subtract 1 from both numbers

 Solution:
45+1: 00101101 -45+1: 10101101
+00000001 +00000001
0 0 1 0 1 1 1 0 = 46 1 0 1 0 1 1 1 0 = - 46

45-1: 00101101 -45-1: 10101101


- 00000001 - 00000001
0 0 1 0 1 1 0 0 = 44 1 0 1 0 1 1 0 0 = - 44

Problem!!!!
2016 EE1501 – Digital Logic 19
4. Numerical Data

Signed magnitude
 Problems with the signed magnitude representation

 Signed magnitude notation does not follow the conventional rules


of arithmetic.

 Two possible expressions for 0:


• 0 = 00000000
• (-0) = 10000000

 Not suitable to design system requiring arithmetic operation but


may be ok when no arithmetic needed (for example, comparator)

 Other method have been developed:


• 1’s complement
• 2’s complement
2016 EE1501 – Digital Logic 20
4. Numerical Data

1’s complement

 Definition: consider a n-bit positive number, N. –N is obtained by


subtracting N from 2n-1.
 To find –N, it is enough to complement the binary number
associated to N

 Example: express the value -45 using 1’s complement


45 = 00101101 -45 = 11010010

 What happen for 0?


0 = 00000000 -0 = 0 = 11111111

 Finally: calculate -45 + 1 and -67+(-60)


-45 + 1 = 11010010 + 00000001 = 11010011 = -44
-67 + (-60) = 10111100 + 11000011 = (1)01111111 = +127 Problem!!

2016 EE1501 – Digital Logic 21


4. Numerical Data

Two’s complement

 Problem with zero:


Idea: add one to 1’s complement binary number (2’s complement)

 Procedure to find binary expression of a negative number:


• Start with the positive number (or magnitude)
• Complement each bit (a “0” becomes a “1” and a “1” becomes a “0”)
• Add one to the result
• If the addition result in a carry-out, discard it
 MSB is still used as the sign bit
 What happen for zero?
0 = (00000000)2 It works!!
(-0) = 11111111+00000001=(00000000)2

2016 EE1501 – Digital Logic 22


4. Numerical Data

Two’s complement

 Example 3: Represent –45 as an eight bit, 2’s complement


number
45 = 00101101
Invert positive number = 11010010
Add 1 to result = 11010011

Thus -45 in 2’s complement form is: 11010011

Note 1: An 8 bit, 2’s complement number can represent integer


values between -128 and +127.
Note 2: Be careful, if you try to find the decimal number from a 2’s
complement binary number. If the MSB is 0, use the normal
technique based on powers of 2. If the MSB is 1, you should find
the positive number and add the negative sign.
2016 EE1501 – Digital Logic 23
4. Numerical Data

Two’s complement

 Let’s try again Example 2:

• 45+1: 00101101 -45+1: 11010011


+00000001 +00000001
0 0 1 0 1 1 1 0 = 46 1 1 0 1 0 1 0 0 = - 44
What is the decimal equivalent of 11010100? As MSB = 1, we must complement
and add 1 to find the magnitude, i.e. :
11010100 00101011 00101100 = 44
Therefore: 11010100 = -44

• 45-1: 00101101 -45-1: 11010011


- 00000001 - 00000001
0 0 1 0 1 1 0 0 = 44 1 1 0 1 0 0 1 0 = - 46
What is the decimal equivalent of 11010010? As MSB = 1, we must complement
and add 1 to find the magnitude, i.e. :
11010010 00101101 00101110 = 46
It works too!!
Therefore: 11010100 = -46

2016 EE1501 – Digital Logic 24


4. Numerical Data

Exercises

 Convert 11010111 (2’s complement) in decimal;

 Convert 1011001110 (unsigned) in decimal and in hexadecimal;

 Convert 86 and 155 in binary and then in hexadecimal;

 Convert -93 in 2’s complement binary and then in hexadecimal;

 Convert 7DH in binary and then in decimal;

 Convert ABCH in binary and then in decimal;

2016 EE1501 – Digital Logic 25


5. Real Numbers

Real numbers

 So far all the numbers we have manipulated were integers.


 How can we represent a real (fractional) number?
 Two method exists:
• Fixed-point representation
• Floating-point representation

 In today computers the floating point representation is the only


one used.
 However, in digital systems, both representation can be used
depending the specifications
 Floating point representation is out of the scope of this module but
information can be found in references or online.

2016 EE1501 – Digital Logic 26


5. Real Numbers

Fixed-point representation

 Each number has exactly the same number of bits.

 Some of these bits represent the integer part

 The rest of the bits represent the fractional part

 Example: n bits for integer part and m bits for fractional part
n 1
Value  i
b .2
i  m
i
Integer part Fractional part

 bn 1 2 n 1  bn  2 2 n  2    b1 21  b0 20  b1 2 1    b m 2  m
 bn 1bn  2  b1b0 .b1 b m

where bi can take the value ‘0’ or ‘1’


2016 EE1501 – Digital Logic 27
5. Real Numbers

Range and resolution

 Range: difference between the maximum and minimum decimal


values that can be coded with the digital code used (it can also be
expressed as the minimum and maximum values)

 Resolution: difference between two consecutive decimal values


represented in binary.

 Examples:
Find the range and resolution of a 3.1 fixed point representation.

Range: 7.5
Resolution: 0.5

2016 EE1501 – Digital Logic 28


5. Real Numbers

Fixed-point representation

 Integer arithmetic is a form of fixed point representation. The


decimal point is at the right of the LSB

 The point does not exist in a digital system, it is only for the
designer/programmer.

 Problems with fixed point representation:


• Restrict the range
• and restrict the resolution of numbers that the system can represent

 Example: Express 1010.01 as decimal number and estimate the


range and resolution of the 4.2 fixed point representation.

2016 EE1501 – Digital Logic 29


6. Arithmetic Operations

Arithmetic Operations

 Knowing how to define a number depending if it is an integer or a


real number is important.

 But to understand how to manipulate numbers, it is more


important to examine how arithmetic operations are applied to
binary numbers

 As with decimal numbers, there are four fundamental operations:


• Addition / Subtraction
• Multiplication / Division

 In this module, we will focus on Addition/Subtraction

2016 EE1501 – Digital Logic 30


6. Arithmetic Operations

Addition

 Addition of binary numbers is performed in a similar manner to


addition of decimal numbers
 Addition rules

0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 0 carry 1
(the carry is added to the next column on the left)

 Example 1: Add the two unsigned integers 1001 and 0011.

2016 EE1501 – Digital Logic 31


6. Arithmetic Operations

Addition

 Example 2:
• Convert -3 and -8 into 8-bit signed two’s complement
• Add both 2’s complement number
• Convert to decimal to check the answer.

 Example 3:
• Convert -1 and 35 into 8-bit signed two’s complement
• Add both number and convert back to decimal

2016 EE1501 – Digital Logic 32


6. Arithmetic Operations

Subtraction

 Subtraction rule
0 - 0 = 0
borrow 1 (1) 0 - 1 = 1
1 - 0 = 1
1 - 1 = 0

(the borrow is coming from the next column on the left)

 Subtraction can also be seen as adding two numbers with one, at


least, being negative.
 For example, in decimal arithmetic: 5-3 can also be written 5+(-3)

2016 EE1501 – Digital Logic 33


6. Arithmetic Operations

Subtraction

 The procedure to subtract two integer numbers can be


decomposed as:
• the number to be subtracted is first inverted using 2’s complement. In the
above example 3 becomes -3
• Then we add both number, e.g: 5+(-3)

 Example 1: Perform 24-9


• Convert 9 into -9 using 2’s complement
• Add the binary equivalent of 24 and -9
• Convert into decimal and
• Use direct subtraction and compare with the previous result

 Example 2:
• Perform 11-14

2016 EE1501 – Digital Logic 34


7. Shift/Rotation

Other operation: Logic Shift

 Logic shift: All the bits are shifted to the left or right.
And a 0 is placed where there is an empty space.
(left means from LSB towards MSB and right from MSB towards LSB)

Note: The MSB is lost on a left shift and the LSB on a right shift. If the number is
signed then a left shift loses the sign of the number

Left shift

1 1 1 0 0 1 0 0 0
1

Right shift
0
1 1 0 0 1 0 0 1 1

2016 EE1501 – Digital Logic 35


7. Shift/Rotation

Other operation: Arithmetic Shift

 Arithmetic shift: does not shift the MSB thus the sign is preserved
but shift the LSB.
Note: Arithmetic left-shift does not shift the sign bit and places a zero in the LSB. On
the other hand arithmetic right-shift fills the spaces left by the shift with the
value of the sign bit. This has the effect of halving the value irrespective of its
sign

Left shift

1 1 0 0 1 0 0 1
0

Right shift
1 1 0 0 1 0 0 1 1

2016 EE1501 – Digital Logic 36


7. Shift/Rotation

Other Operations: Rotate

 Rotate: It is a circular shift. Therefore no bit is lost as in the logic


and arithmetic shift.
Note: In the case of a right rotation, the bits are moved to the right and the LSB
became the MSB.
For a left rotation, the bits are shifted to the left and the MSB becomes the
LSB.
 Example: Consider the binary number 10010110

1 1 0 0 1 0 1 1 0 Rotate left

1 0 0 1 0 1 1 0 0 Rotate right

2016 EE1501 – Digital Logic 37


8. Design Issues

Design issues

 When designing a digital system, you may have to calculate the


number of bit and the format of binary numbers.

 Specifications should be your main source of information.

 From the resolution, you can determine the number of digit for the
decimal part.

 From the minimum and maximum values expected, you can find
the number of bit needed for the integer part.

 Example:
• Determine the minimum format for a system that must have a range of 10
(0-10) and a resolution of 0.5.

2016 EE1501 – Digital Logic 38


8. Design Issues

Design issues: Exercises

 Exercise: Let’s consider sensors measuring Electrocardiography of a


patient. The signal measured looks like:

After amplification, the measured signal is between -5mV and 20mV.


If we forget about the unit, how many bits are needed to cover the
range, -5 to 20 with a resolution of 0.01.

2016 EE1501 – Digital Logic 39


8. Design Issues

Some issues with arithmetic operations

 Overflow is one of the most critical problem when dealing with


binary numbers and/or arithmetic
• Consider the second example on the previous slide. If the number of bit is
reduce by 1 (i.e. 4.4), some values will be out of range, thus creating and
overflow if such value appears.
• Consider an addition, it is common to discard the carry for the MSB. This is
overflow. The result will not be the real value.
• You can also have overflow due to multiplication as the multiplication of
integer numbers double the number of bits.
 One of the main reasons of overflow is a number of bit too small to
represent all the values (should be considered during design).
 The second issue is rounding. It is the action of approximating a
value due to the binary number format.

2016 EE1501 – Digital Logic 40

You might also like