Vous êtes sur la page 1sur 17

Next: Two's Complement Representation Up: arithmetic_html Previous: Number Systems

Number System Conversion

Binary to Decimal (See before)

Decimal to Binary, Octal, Hexadecimal Example:

So, we have

A systematic method for number conversion

Why does it work? Given a value in a certain number system, we want to convert it to a , i.e., we need to find all 's in the expression:

different system of radix

The value of the integer part of the above expression is

Dividing this by

, we get

as the remainder. If we keep dividing the 's can be found as the

previous quotient by remainders.

, the subsequent is

The value of the fraction part of

Multiplying this by

, we get

as the integer part of the product. If we keep , the subsequent

multiplying the fraction part of the previous product by 's

can be found as the integer part of the products.

Note: This method is mostly applicable to conversion of values given in decimal (base 10) system to other systems (base r=2, 8, 16, etc.), simply because we are trained to multiply and divide only in decimal system. In theorey one could use this method to convert a value given in any number system, but one has to know how to multiply and divide in that particular system. More Examples:

Binary to Octal Example:

Octal to Binary Example:

Binary to Hexadecimal Example:

Hexadecimal to Binary Example:

Binary-Coded-Decimal (BCD) Numbers Example:

Next: Two's Complement Representation Up: arithmetic_html Previous: Number Systems Ruye Wang 2003-10-24

Next: Fixed-Point Representation Up: arithmetic_html Previous: Number System Conversion

Two's Complement Representation

Decimal - 10's Complement (n-digit) Example:


o

Binary - 2's Complement (n-bit) Example:


o

Note:

An n-bit binary number value in the range that (e.g., if

can represent any integer , then . ). Note

bits are needed to represent the value

The highest (left-most) bit

of an n-bit number is called the most

significant bit (MSB) and the lowest bit (right-most) the least significant bit (LSB). Complement representation is always determined by the number of digits or bits in the representation. Given n bits, is treated as 0 since the 1 in the (n+1)th bit cannot be represented (e.g., , ). Complement of zero: (e.g., Complement of complement: , ).

Sign extension: any number of 0's can be added in front of a number without changing its value. Similarly, any number of 1's can be added in front of the 2's complement representation, without changing its value. Decimal sign extension:

Binary sign extension:

Purpose of complement representation: The 2's complement representation is used to deal with negative numbers and subtractions, because complement is compatible with negation: , , as shown above.

Example 1: Carry out decimal subtraction using digits.

The 1 in the 4th digit is automatically dropped as only Example 2: Carry out binary subtraction in hardware for representing a number. , with

digits are available. bits available

Note that the 7th bit is automatically dropped as only bits are available in hardware. This example shows subtraction can be carried out as an addition of 2's complement of the second number. The question is, how can the 2's complement be easily found?

Three ways to find 2's complement: Given an n-bit binary number found by one of the following equivalent ways: , its 2's complement can be

1. Carry out the subtraction in the definition 2. First find bit-wise complement ( , complement), then add 1 to the LSB Example: ;

) of

; (called 1's

Note that

, i.e., bit-wise complement.

3. Leave all least significant 0's and the first 1 unchanged, then bit-wise complement the remaining bits. Example:

Note that the least significant three bits 100 are unchanged. Observation on the MSB of 2's complement Assume bits available:

If and

, then are represented by

, i.e., both with MSB=0.

If

, then

i.e., both

and

are represented by

with MSB=1.

If
o

, then The MSB of is 0:

The MSB of

is 1:

Conclusion:

If If

, the MSB of , the MSB of

is always 0;

is always 1;

Example:

Next: Fixed-Point Representation Up: arithmetic_html Previous: Number System Conversion Ruye Wang 2003-10-24

Next: Signed-Complement Representation for Fractions Up: arithmetic_html Previous: Two's Complement Representation

Fixed-Point Representation
Signed 2's complement
A fixed point number (an integer) defined as:

can be represented by signed 2's complement,

If

, it is represented in binary form, but the MSB must be 0. , but the

If , it is represented by 2's complement of its magnitude MSB must be 1.

The MSB can therefore be used as the sign bit. Both cases can be represented by the following:

Note:

Assume there are a fixed number of n bits available in hardware for representing a given value. When , in the expressions above corresponds to a 1 in the (n+1)th bit, which is always ignored in n-bit representation as the (n+1)th bit does not exist. The remaining is in binary form. When , is the definition of 2's complement of .

Range of integer values representable Example: , , ,

In this example, the range of values representable by 3-bit signed 2's complement is from 0 to for positive values and from values. In general, we note:

to -1 for negative

When

represents the plus sign, and the remaining n-1 bits .

represent the magnitudes in the range

When

represents the minus sign, and the remaining n-1 ,

bits represent the magnitude in the range

i.e. . The MSB is thus used as the sign-bit to indicate whether the number is positive or negative. The Overall range for Signed-Complement representation is: ;

Any value beyond this range cannot be properly represented as the MSB no longer correctly indicates whether

or

Signed-2's complement representation simplifies the operations when dealing with negative operands and subtractions.

When these the

values are represented by signed 2's complement, MSB=1 for to -1), MSB=0 for the values on the

values on the left (from ).

right (from 0 to Examples:

* This is an overflow, as when to 15.

, the range of values representable is from -16

Find value represented in signed-2's complement To find the value of a given number complement format, check the MSB or the sign bit

in signed 2's :

If

, the value is positive and represented in binary form:

If

, the value is negative and its magnitude can be found ):

by complementing the given representation (

Both cases can be represented by this formular:

Example (n=6):

Next: Signed-Complement Representation for Fractions Up: arithmetic_html Previous: Two's Complement Representation Ruye Wang 2003-10-24

Next: Addition/Subtraction Up: arithmetic_html Previous: Fixed-Point Representation

Signed-Complement Representation for Fractions


Assuming there are n bits available, and a fraction can be represented by assuming the decimal point to be between the two most significant bits and . And the signed-complement representation of a fraction is the same as that of the integers:

If

, it is represented in binary form, but the MSB must be 0.

If , it is represented by 2's complement of its magnitude MSB must be 1. ,

, but the

Example 1: But magnitude values are Example 2:

is represented by is represented by the 2's complement of its , i.e., and

. For , the representations of these two , respectively.

Given values is

bits, there are always

different values representable. The range of these . For example,

Ruye Wang 2003-10-24

Vous aimerez peut-être aussi