hexadecimal number system
Download
Report
Transcript hexadecimal number system
Objectives
• Learn why numbering systems are important to
understand
• Refresh your knowledge of powers of numbers
• Learn how numbering systems are used to
count
• Understand the significance of positional value
in a numbering system
• Learn the differences and similarities between
numbering system bases
Objectives (continued)
• Learn how to convert numbers between
bases
• Learn how to do binary and hexadecimal
math
• Learn how data is represented as binary in
the computer
• Learn how images and sounds are stored
in the computer
Why You Need to Know About...
Numbering Systems
• Computers store programs and data in
binary code
• Understanding of binary code is key to
machine
• Binary number system is point of departure
• Hexadecimal number system
– Provides convenient representation
– Written into error messages
Powers of Numbers - A
Refresher
• Raising a number to a positive power (exponent)
– Self-multiply the number by the specified power
– Example: 23 = 2 * 2 * 2 = 8 (asterisk =
multiplication)
– Special cases: 0 and 1 as powers
• Any number raised to 0 = 1; e.g, 10,5550 = 1.
• Any number raised to 1 = itself; e.g., 10,5551 =
10,555
Powers of Numbers -A
Refresher (continued)
• Raising a number to a negative power
– Follow same steps for positive power
– Divide result into 1; e.g., 2-3 = 1/ (23) = .125
Counting Things
• Numbers are used to count things
– Base 10 (decimal) most familiar
• The computer uses base 2, called binary
– Base 2 has two unique digits: 0 and 1
Base 10
• Example 12:
• Express Decimal 42.56 using radix of 10.
4 2 . 5 6
4 10
4 101 +
+
2 1 + 5 0.1 +
2 100 +
5 10- 1 +
6 0.01
6 10-2
Counting binary numbers
BinarytoDecimal Conversion
• Any binary number can be converted to
decimal by multiplying the weight of each
position with the binary digit and adding
together.
Binary number
1
0
1
1
0
Power of 2 position
24 +
23
+
(241) + (230) +
Decimal value
22
(221) +
16 + 0 + 4 + 2 + 0
= 2210
+
21
+
(211) +
20
(20 0)
Examples
• Convert the following Binary numbers to
its equivalent Decimal number.
• (i) 10110
• (ii) 110101
• (iii) 111001.0101
• (iv) 111111.111
Decimal–to–Binary Conversion
• The most convenient method is called division by 2
method.
• In which first decimal number will be divided by 2.
• The quotient will be dividend for the next step.
• In each step the remainder part will be recorded
separately.
• The 1st remainder of the 1st division will be the LSB in
the binary number.
• The quotient should repeatedly divide by 2 until the
quotient becomes 0.
• The final remainder will be the MSB in binary number.
Decimal–to–Binary Conversion
2
20
+
remainder of
0
2 10
+
remainder of
0
2
5
+
remainder of
1
2 2
+
remainder of
0
2 2
+
remainder of
1
0
1
0
0
1
0
02
• Converting a decimal fractional number to its binary the
decimal fractional part will be multiply by 2 till the
fractional part gets 0 or till the number of decimal places
reached.
0.25 2 = 0.50
MSB
0.5 2 = 1.00
LSB
. 0
0.2510 = . 012
1
Examples
• Convert the following Decimal numbers to
its equivalent Binary number.
• (i) 47
• (ii) 254
• (iii) 12.5
• (iv) 341.58
Binary Addition
• Adding of two binary numbers follows
same as addition of two decimal numbers.
Sum
Rule 1
Rule 2
Rule 3
Rule 4
0
0
1
1
+
+
+
+
0
1
0
1
=
=
=
=
0
1
1
0
Carryout
0
0
0
1
Binary Addition
• Procedure for adding numbers similar in all bases
– Difference lies in carry process
– Value of carry = value of base
– Example: 1011
–
+1101
–
11000
– Carry value for above = 102 = (1 x 101 + 0 x 100 ) =
210
• Procedure for subtraction, multiplication, and division
also similar
Examples
• .
•
•
•
Perform the following binary addition.
(i) 1011 + 0101
(ii) 111101 +010110
(iii) 11101.01 +1010.110
Binary Subtraction
• When subtracting one binary number A
(subtrahend) from another binary number
B (Minuend) where B > A, the answer is
called the difference. There are four basic
Subtrahend (A)
Difference
Borrow out
rulesMinuend
that(B)should
be followed
in binary
Rule
1 0
0
=
0
subtraction
Rule 2 0
Rule 3 1
Rule 4 1
1
0
1
=
=
=
1
1
0
with a borrow of 1
Binary Subtraction
Examples
•
•
•
•
Perform the following binary subtraction.
(i) 110 – 010
(ii) 1110-0111
(iii) 110001-01111
HEXADECIMAL NUMBER SYSTEM
• Hexadecimal number system is called as
base 16 number system and it uses 10
decimal numbers and 6 alphabetic
characters to represent all 16 possible
symbols.
Converting To Base 10
• Three methods:
1. Table look-up (more extensive than Table 41)
2. Calculator
3. Algorithm for evaluating number in any base
• Example: consider 169AE in base 16
– Identify base: 16
– Map positions to digits: 4 3 2 1 0
– Raise, multiply and add: 169AE = (1 x 164)
+ (6 x 163) + (9 x 162) + (10 x 161) + (14 x
160) = 92,590
Examples
• Convert the following Hexadecimal
numbers to its equivalent Decimal number
•
(i) 79
•
(ii) 6A.9
•
(iii) AB4.95C
•
(iv) ABDC.FFC
Converting From Base 10
• Three methods:
1. Table look-up (more extensive than Table 41)
2. Calculator
Converting From Base 10
(continued)
3. Algorithm for converting from base 10
1. Divide the decimal number by the number of the
target base (for example, 2 or 16)
2. Write down the remainder
3. Divide the quotient of the prior division by the
base again
4. Write the remainder to the left of the last
remainder written
5. Repeat Steps 3 and 4 until the whole number
result is 0
Converting From Base 10
(continued)
• Practice conversion algorithm: find
hexadecimal equivalent of decimal 45
– Divide 45 by 16 (base)
– Write down remainder D
– Divide 2 by 16
– Write down remainder 2 to the left of D (2D)
– Stop since reduced quotient = 0
– Check: 2D = (2 x 161) + (13 x 160) = 32 + 13
= 45
Examples
• Convert the following Decimal numbers to
its equivalent Hexadecimal number
•
(i) 67
•
(ii) 195.67
•
(iii) 597.96
•
(iv) 1022.5845
HexadecimaltoBinary
Conversion
• Hexadecimal number can be represent in
binary form by using 4 bits for each
hexadecimal number.
• 016 can be written in binary – 0000
• 716 in binary can be written – 0111
• A16 in binary can be written – 1010
Examples
• Convert the following Hexadecimal
numbers to its equivalent Binary number.
•
(i) 6F
•
(ii) 5ABD
•
(iii) 8A.D9
•
(iv) 9FFF.FEC
BinarytoHexadecimal
Conversion
• This is the reverse form of the
hexadecimal to binary conversion. First
the binary number should be divided into
group of four bits from the LSB. Then each
four-bit binary number is converted to a
hexadecimal form
Examples
• Convert the following binary number to its
equivalent Hexadecimal number.
•
(i) 11010101
•
(ii) 1011101010
•
(iii) 10110101.001010
Hexadecimal Addition
• The addition in hexadecimal is same as in binary
addition. The largest single digit in octal is 15. If the
result of A + B is greater then 15, then we must subtract
16 (the base) and carry 1 to the next digit.
• Lets look some how to do additions in octal
• 016 +
416 =
410
= 416
• 416 +
B16 =
1410
= E16
• 416 +
C16 =
1610 >15 Subtract 16 and
carry 1
=1016
Examples
• Perform the following Additions in
Hexadecimal.
•
(i) 948 + A39
•
(ii) ABD6 + 9FFE
•
(iii) ABCD.9F + 99FF.FE
Counting Things (continued)
• Hexadecimal system used to represent
binary digits
– Base 16 has sixteen unique digits: 0 – 9, A - F
• Counting for all number systems similar
– Count digits defined in number system until
exhausted
– Place zero in ones column. Carry one to the
left
DATA REPRESENTATION
• Binary numbers can be stored in two
different formats :
• Unsigned numbers
• Signed numbers
Binary representation
Unsigned
Signed
Signed
magnitude
One's
complement
Two's
complement
Integer Representation
• To represent an integer in fixed point
representation the radix point is fixed. For
example, a 8 bit register can hold at the
maximum of 8 bits and a 16 bit register can hold
16 bits.
UNSIGNED NUMBERS
• If a number is represented in unsigned, it is
assumed to be positive. So it is not necessary to
indicate the sign bit. All the bits including the
sign bit is used to store the magnitude.
Example 3–1
Represent the decimal number 52 in 16 bit unsigned representation
Solution
The binary form of 52 is 110100. Note that the number of bits for the
binary number 52 is only 6.
Fill the empty positions (10 positions) on the left with zeros as
0000000000110100.
SIGNED NUMBERS
• Signed numbers can be represented in
any one of the following ways :
Signed magnitude
• One’s (1’s) Complement
• Two’s (2’s) Complement
Signed Magnitude Representation
• In signed magnitude, the MSB is reserved for sign. If the
MSB is 0 then the number is positive. If the number is
negative, then the MSB will be set to 1.
• Reserving the sign bit will restricts the maximum range
of the values.
• For example, if the storage is 8 bit, only 7 bits are used
for magnitude (0-6). The MSB is reserved for the sign bit
(7th bit).
• If the system uses a 8 bit storage, then the range is from
-127 to +127 (11111111 to 01111111).
Signed Magnitude
• The following procedure is followed to represent
a decimal number in signed magnitude:
• Temporarily ignore the sign of the given decimal
number.
• Convert the absolute value (magnitude) of the
number into its binary equivalent.
• Fill in the empty positions on the left with zeros
except MSB.
• Check the sign of the given decimal number. If
the number is positive fill the MSB with 0. If the
number is negative fill the MSB with 1.
Signed Magnitude
• Example 3–2
Represent the decimal number +15 in 8 bit signed
magnitude representation
• Solution
The absolute value of the given decimal number is 15.
The binary equivalent of the decimal number 15 is 1111.
Fill in the empty positions on the left with zeros except
MSB is 0001111.
The sign of the given number is positive. Hence, fill in
the MSB as 0, which gives 00001111 is the result.
Signed Magnitude
• Example 3–3
Represent the decimal number –15 in 8 bit signed
magnitude representation
• Solution
The absolute value of the given decimal number is 15.
The binary equivalent of the decimal number 15 is 1111.
Fill in the empty positions on the left with zeros except
MSB is 0001111.
The sign of the given number is positive. So fill in the
MSB as 1, which gives 10001111 is the result.
Examples
• Convert the following decimal numbers to
their 8bit sing binary format
• (i) +36
• (ii) -69
• (iii) +95
• (iv) –120
One’s (1’s) Complement Representation
• The following procedure is followed to represent a
decimal number in 1’s complement representation:
• Temporarily ignore the sign of the given decimal number
• Convert the absolute value (magnitude) of the number
into its binary equivalent
• Fill all the empty positions on the left with zeros including
MSB
• Check the sign of the given decimal number. If the
number is positive stop the process and the number
obtained in the third step is the result. If the number is
negative, then complement the digits as 0 to 1 and 1 to 0
One’s (1’s) Complement Representation
• Example 3–4
Represent +15 in 1’s complement representation using 8
bit storage
• Solution
The magnitude of the given decimal number is 15.
The binary equivalent of the decimal number 15 is 1111.
Fill all the empty positions on the left with zeros including
MSB as 00001111.
The sign of the given number is positive. Hence, 1’s
complement representation of the given number +15 is
00001111.
One’s (1’s) Complement Representation
• Example 3–5
Represent –15 in 1’s complement representation using 8
bit storage
• Solution
The absolute value of the given decimal number is 15.
The binary equivalent of the decimal number 15 is 1111.
Fill all the empty positions on the left with zeros including
MSB as 00001111.
The sign of the given number is negative. Therefore,
complement the number which has been obtained in
third step. The resultant representation is 11110000.
Examples
• Represent the following numbers in their
1s complement representation.
• (i) +76
• (ii) –90
• (iii) –105
• (iv) -124
Two’s (2’s) Complement Representation
In 2’s complement representation the MSB is reserved for
sign bit.
The following procedure is followed to represent a decimal
number in 2’s complement representation:
Temporarily ignore the sign of the given decimal number.
Convert the absolute value of the number into its binary
equivalent.
Fill all the empty positions on the left with zeros including
MSB.
Check the sign of the given decimal number. If the number
is positive stop the process and the number obtained in
the third step is the result. If the number is negative, then
complement the digits as 0 to 1 and 1 to 0. After inverting
the individual digits add 1 to LSB. If adding 1 result in a
carry from the MSB, that carry is dropped.
Two’s (2’s) Complement Representation
• Example 3–6
Represent +15 in 2’s complement representation using 8
bit storage
• Solution
The absolute value of the given decimal number is 15.
The binary equivalent of the decimal number 15 is 1111.
Fill all the empty positions on the left with zeros including
MSB as 00001111.
The sign of the given number is positive. Hence, 2’s
complement representation of the given number +15 is
00001111.
Two’s (2’s) Complement Representation
• Example 3–7
Represent the decimal number –15 in 2’s complement
representation using 8 bit storage
• Solution
The absolute value of the given decimal number is 15.
The binary equivalent of the decimal number 15 is 1111.
Fill all the empty positions on the left with zeros including
MSB as 00001111.
The sign of the given number is negative. So,
complement the number which has been obtained in
third step. Inverting the individual digits of the number
00001111 yields 11110000. Now add 1 to 11110000
which produces 11110001.
Examples
• Represent the following numbers in their
2s complement representation.
•
(i) + 69
•
(ii) – 80
•
(iii) –100
•
(iv) -111
FLOATING POINT REPRESENTATION
• Fixed point representation is well suited to
represent range of positive and negative
integers but it has some disadvantages
such as
– Integers with large magnitude can not be
represented
– If the division algorithm uses very large
dividend and divisor, then the fractional part of
the quotient may be lost
Floating Point Representation
• In floating point form, the radix point can be
moved to different position to represent the
number in convenient way. For example
consider a decimal number +7321.621. This
number can be represented in different ways by
moving the position of the radix point to different
locations.
+7321.621 is equivalent to +0.7321621
10+4
+7321.621 is equivalent to +7.321621
10+3
+7321.621 is equivalent to +73.21621 10+2
Floating Point Representation
• The same approach is also used to
represent the binary numbers. In general,
a number can be represented in floating
point form with the following notation:
C R E
Where, C is a coefficient, R is a radix or
base, and E is an exponent.
Normalization
• If the most significant position of the coefficient is zeros
then normalization is required.
• Normalization is a process to shift the radix point so that
the coefficient has no leading zeros and very useful to
avoid storing of unnecessary zeros.
•
For example, 0.00011011 24 requires normalization.
Since the coefficient has three leading zeros. The
normalization for this number is shown below:
The number before normalization 0.00011011 24
The number after first normalization 0.0011011 23
The number after second normalization 0.011011
22
The number after third normalization 0.11011 21
IEEE Standard for Floating Point Representation
• Nowadays, the most sophisticated
processors uses IEEE-754, a well defined
and universally adopted standard for
representing binary floating point numbers.
• This standard defines two formats such
as single precision format which is 32-bits
and double precision format having 64bits.
Single Precision format
• Single precision format uses 32-bits to represent
the floating point numbers.
• One bit is reserved for sign bit, eight bits reserved
for biased exponent and 23 bits reserved for
fraction. In single precision, the bias value is 127.
The following Figure 3−6 depicts the bit format for
single precision representation
1 bit
Sign bit (S)
8 bits
Biased Exponent (E)
Figure 3−6: IEEE Single precision format
23 bits
Fraction
Representing Characters
• Computers store characters according to standards
• ASCII
– Represents characters with 7-bit pattern
– Provides for upper and lowercase English letters,
numeric characters, punctuation, special characters
– Accommodates 128 (27) different characters
• Globalization places upward pressure
– Extended ASCII: allows 8-bit patterns (256 total)
– Unicode: defined for 16 bit patterns (34,168 total)
American Standard Code for Information Interchange
(ASCII)
Examples
•
Convert the following message to its
ASCII code.
•
•
Total cost = $450/=
Will be back tomorrow at 10.00pm
Representing Images
• Screen image made up of small dots of colored light
– Dot called “pixel” (picture element), smallest unit
– Resolution: # pixels in each row and column
– Each pixel is stored in the computer as a binary
pattern
• RGB encoding
– Red, blue, and green assigned to eight of 24 bits
– White represented with 1s, black with 0s
– Color is the amount of red, green, and blue specified
in each of the 8-bit sections
Representing Images
(continued)
• Images, such as photos, stored with pixelbased technologies
• Large image files can be compressed
(JPG, GIF formats)
• Moving images can also be compressed
(MPEG, MOV, WMV)
Representing Sounds
• Sound represented as waveform with
– Amplitude (volume) and
– Frequency (pitch)
• Computer samples sounds at fixed intervals
– Samples given a binary value according to amplitude
– # bits in each sample determines amplitude range
– For CD-quality audio
• Sound must be sampled over 44,000 times a
second
• Samples must allow > 65,000 different amplitudes
One Last Thought
• Binary code is the language of the
machine
• Knowledge of base 2 and base 16
prerequisite to knowledge of machine
language
• Computer scientists are more effective
with binary and hexadecimal concepts
Summary
• Knowledge of alternative number systems
essential
• Machine language based on binary system
• Hexadecimal used to represent binary
numbers
• Power rule for numbers defines selfmultiplication
Summary (continued)
• Positional value: weight based on digit position
• Counting processes similar for all bases
• Conversion between bases is one-to-one mapping
• Arithmetic defined for all bases
• Data representation: bits, nibbles, bytes, words
Summary (continued)
• Two’s complement: technique for storing signed
numbers
• Floating point notation: system used to represent
fractions and irrationals
• ASCII and Unicode: character set standards
• Image representation: based on binary pixel
• Sound representation: based on amplitude
samples
Test Yourself
• Download the “Test_yourself_Ch04. Pdf”
from Interact
• Answer all the questions
Connecting with Computer
Science
71