Vous êtes sur la page 1sur 5

Student Notes Theory

Computer Logic 2 Further reading: JW pg 276

What is a Register?

In a previous topic we have seen that a register is a, special, high-speed storage


area within the CPU. All data must be represented in a register before it can be
processed. For example, if two numbers are to be multiplied, both numbers must be in
registers, and the result is also placed in a register. (The register can contain the
address of a memory location where data is stored rather than the actual data itself.)

A REGISTER is a storage location with a fixed number of bits. Hence, in an 8-bit


register, the number 1 will be stored as:
00000001

Numerical Overflow

NUMERIC OVERFLOW occurs when the result of a computation (calculation) is outside


a fixed range, such as that of a register.

Example: In order to store a 9-bit number (101010000) in an 8-bit register, two 8-bit
registers have to be taken up (i.e. it would overflow if put in one 8-bit register:

Two 8-bit registers 00000001 01010000

Left Shift

Shifting a binary number left by 1 bit multiplies it by 2.

Shifting a binary number left by 1 bit means inserting a 0 to the right, so that this
becomes the new least significant bit:

1011002 = 4410 10110002 = 8810

Right Shift

Shifting a binary number right by 1 bit halves it.

Shifting a binary number right by 1 bit NOTE THAT WHEN SHIFTING A BINARY
means deleting the present least significant NUMBER TO THE RIGHT, AND A 1 IS DELETED,
bit, irrespective of whether this is a 1 or a 0, SINCE THAT WAY DATA IS LOST, THE VALUE
so that the bit next to it, (on the left), MAY NOT BE EXACTLY HALVED.
becomes the new significant bit:

1001112 = 3910 100112 = 1910

Page 1 K Aquilina
Student Notes Theory

Binary Addition
When adding binary numbers we must keep in mind that we can use only the binary
digits 1 and 0. In decimal addition we carry 1 when the total of a column is 10 or more
since the base is 10. In binary addition, we carry 1 when the total of a column is 2 since
the base is 2.

• 0 + 0 = 0
• 0 + 1 = 1
• 1 + 0 = 1
• 1 + 1 = 0, and carry 1 to the next more significant bit
• 1 + 1+ 1 = 1, and carry 1 to the next more significant bit

Exercise 1:

00011010 + 00001100 = carries


0 0 0 1 1 0 1 0 = 26(base 10)
+ 0 0 0 0 1 1 0 0 = 12(base 10)

= 38(base 10)

00010011 + 00111110 = carries


0 0 0 1 0 0 1 1 = 19(base 10)
+ 0 0 1 1 1 1 1 0 = 62(base 10)

= 81(base 10)

Unsigned Integers
Unsigned integers are positive numbers together with zero ( 0 ).

Register Size No. of Possible Largest binary Range


Codes number
2 bits ( 22 = ) 4 112 = 310 0-3

3 bits =

4 bits =

5 bits =

6 bits =

N bits -------------------

How may Negative numbers be held in the binary system?

Page 2 K Aquilina
Student Notes Theory

Signed Integers
Signed integers are ALL whole numbers i.e. Positive, Negative numbers and Zero.

Signed numbers can be represented by using one of the following methods;


1. Sign and Magnitude
2. One’s Compliment
3. Two’s Compliment

Sign and Magnitude Representation

Sign-magnitude representation uses the most significant bit of the number to indicate the
sign. A negative number is the 7-bit binary representation of the positive number with the
most significant bit set to one.

The most significant bit represents the sign (positive or negative)

0110 1001
msb is 0 so positive msb is 1 so negative
( 01102 = +610 ) ( 10012 = -110 )

As seen in class, the drawbacks to using this method for arithmetic calculations are that
a different set of rules are required and that zero can have two representations (+0,
0000 0000 and -0, 1000 0000).

Exercise 2:

Convert to decimal the following sign & magnitude binary numbers;


1) 01102 3) 001112
2) 11102 4) 111112

One’s Complement
To convert to 1s complement:
Invert all bits of the number (1s are changed to 0s and 0s to 1s )

Two’s Complement
To convert to 2s complement:
a. Invert all bits of the number (1s are changed to 0s and 0s to 1s)
b. Add 1 (The msb is the sign)

For example,

0001 0001(binary 17) 1110 1111(two's complement -17)

Page 3 K Aquilina
Student Notes Theory

NOT(0001 0001) = 1110 1110 (Invert bits)


1110 1110 + 0000 0001 = 1110 1111 (Add 1)

Property
Two's complement representation allows the use of binary arithmetic operations
on signed integers, yielding the correct 2's complement results.

Positive Numbers
Positive 2's complement numbers are represented as the simple binary.

Negative Numbers
Negative 2's complement numbers are represented as the binary number that
when added to a positive number of the same magnitude equals zero.

Note: The most significant bit indicates the sign of the integer; therefore it is sometimes called the
sign bit. If the sign bit is zero, then the number is greater than or equal to zero, or positive. If the
sign bit is one, then the number is less than zero, or negative.

0000
1111
-1 0 0001
1110 -2 1
0010
1101
2
-3
3 0011 2's complement
1100 -4 in a 4-bit register
4 0100
1011 -5
5 0101
1010 -6 6 0110
-7 7
1001 -8 0111
1000 most positive
number
most negative
number

Example

Find the 2’s complement of number - 710;

Convert to binary: + 710 = 01112

1s Compliment (Invert the number): 10002

2s Compliment (Add 1) 1000 + 1 = 10012

Answer: 2’s complement of number - 710 is 10012

Page 4 K Aquilina
Student Notes Theory

Exercise 3:

Convert the following decimal numbers to 8-bit 2s compliment;


1) -7610 3) -2310
2) -3210 4) 5610

2's Complement Subtraction


Two's complement subtraction is the binary addition of the first (positive) binary number
to the 2's complement of the second (negative) number.

Example 1: 5 + (-3)

5 + (-3) = 2 0000 0101 = +5


+ 1111 1101 = -3

0000 0010 = +2

Example 2: 7 - 12

7 – 12 is the same as 7 + (-12), so;

7 - 12 = (-5) 0000 0111 = +7


+ 1111 0100 = -12

1111 1011 = -5

Summary
 Binary notation can be used to store any data type in memory
 We have learned how to represent
o Characters
o Integer numbers (positive and negative numbers)

Notes
Binary Number System
Bit (short for binary digit): A single binary digit 0 and 1
LSB (least significant bit): The rightmost bit
MSB (most significant bit): The leftmost bit

Binary Equivalents
1 Byte = 8 bits
1 Kilobyte (KB) = 1024 bytes = 210 bytes
1 Megabyte (MB) = 1024 kilobytes = 1024 x 1024 bytes
1 Gigabyte (GB) = 1024 megabytes = 1,073,741,824 bytes

Note that the number 1024 = 210

Page 5 K Aquilina

Vous aimerez peut-être aussi