Convert to binary

advertisement
Introduction to
Information Technology
BINARY REPRESENTATION OF REAL NUMBERS AND TEXT
HEXADECIMAL AND OCTAL
Lecture Notes - 2
1
Topics

Representing “Real Numbers” in Binary

Representing Negative Numbers in Binary

Representing Text in Binary


Determining How Many Bits are Needed to
Represent Something
Octal and Hexadecimal Numbering Systems
2
Representing Real Numbers in Bits



We’ve learned how to represent integers in binary form.
Real numbers can also be represented in binary.
We will illustrate this using thermometer measurements as an example.

A mercury thermometer reflects temperature that can continuously
vary over its range of measurement (an analog device).

A digital thermometer would require an infinite number of bits to
accomplish the same thing.

If we are building a digital thermometer, we must make some
choices and determine some parameters:
 Precision (number of bits we will use) v. cost
 Accuracy (how true is our measurement against a given
standard).
3
CONTINUOUS VS. DISCRETE FUNCTIONS

Recall that mercury thermometer measurements are a continuous function:
there is an infinite number of points making up that function. Discrete
functions have a finite number of values.
4
FUNCTIONS

In mathematics, a function is an association between two sets of
values in which each element of one set has one assigned element in
the other set so that any element selected becomes the independent
variable and its associated element is the dependent variable. Thus, in:
y = f(x)
y is said to be a function of x.


Common functions we might deal with:

Functions of Time

Functions of Space

Functions of Frequency

Functions of Power
Usually, functions are represented as a relationship on an x-y axis
graph.
5
THE SINE WAVE IS A FUNCTION
6
REPRESENTING REAL NUMBERS
Using temperature values as an example:






What is the range we wish to measure?
How many values do we need to provide adequate precision?
How many bits are we willing to allocate?
Suppose we want to represent a range of temperature measurements
between 60-70oF
There is an infinite number of possible different temperature readings
between 60-70oF
Let’s decide that 256 different temperature values between 60-70
would give us more than enough precision.

An 8-bit code would provide these values. Why 8 bits?
70-60
= 0.039oF
256
The 256 temperatures we can represent are separated by .039
7
CONVERTING TEMPERATURE INTO BITS

We can use the 8-bit word to encode the following values:
Binary Codeword Temperature, oF
00000000
60.000
00000001
60.039
00000010
60.078
00000011
60.117
.
.
.
.
11111101
69.883
11111110
69.922
11111111
69.961
8
ANOTHER TEMPERATURE EXAMPLE



Suppose we want to read the thermometer to the hundredth of a
degree between the values of -40 and +140oF.

How many bits are required?

180 x 100 = 18000 steps

Can implement with a 15 bit code (215 = 32768)

Note “wasted” bits
Precision now: to the hundredth degree

Is a precise instrument the same as an accurate one?
How do we build the code now?
9
THERMOMETER CODING - ONE METHOD
140.00º F
139.99º F
139.98º F
139.97º F
0.00º F
-39.98º F
-39.99º F
-40.00º F
100011001010000
100011001001111
100011001001110
100011001001101
000111110100000
000000000000010
000000000000001
000000000000000
10
THERMOMETER CODING
(ANOTHER METHOD)
0.04º F
0.03º F
0.02º F
0.01º F
0.00º F
-0.01º F
-0.02º F
-0.03º F
000000000000100
000000000000011
000000000000010
000000000000001
000000000000000
111111111111111
111111111111110
111111111111101
11
WHAT WAS THAT?

The most common way to represent negative number is using “2’s
Complement Notation.”

Take the positive binary representation with the MSB as a sign bit
(a 0 makes the representation positive)
 01001 (+910)

Take the binary complement (flip the bit values)
 10110

Add a binary 1
 10110 + 1 = 10111 (-910)
12
2’S COMPLEMENT NOTATION

Express the decimal number -5 in 5-bit binary notation.



First determine the 5-bit binary representation of 5:
 00101
Then complement (change 1s to 0s and 0s to 1s) each bit:
 11010
Finally, add 1:
 11011
-5 = 11011 in 2’s complement notation
(To reverse the process take the complement and add 1!)
Note that the 2’s complement of a 2’s complement of a number
equals the original number.
13
IN-CLASS PROBLEMS
14
How Can Alphanumeric Symbols be
Represented in Binary Code?
American Standard Code for Information Interchange
Information is entered
via a keyboard.
For example “Hi”

The computer
translates each typed
character into a 7 (or
8) bit binary code.
For example,
10010001101001
How does the computer determine that “Hi” should be translated into
10010001101001? It uses a code, ASCII, which specifies by agreed upon
convention, a unique 7 or 8 bit combination for each alphanumeric character.
15
About ASCII






ASCII is a fixed length code, meaning it uses the same number of
bits to represent all possible symbols produced by the source.
ASCII can represent 128 symbols (27 symbols) and Extended ASCII
can represent 256 symbols (28 symbols).
Computer memories are usually structured in 8-bit units (bytes).
It is convenient to represent alphanumeric symbols with a single
byte for storing text, so ASCII usually uses 7 bits plus an
“extended” bit.” This is called “Extended ASCII.”
Appendix A in the text contains an ASCII key, which specifies a
decimal, hexadecimal, and binary code for each alphanumeric
character.
Using the ASCII key, how would we translate IT 101 into decimal or
binary code?
 Let’s look at the ASCII key….
16
Sample ASCII Chart
17
ASCII Encoding Example
Using the ASCII key in Appendix A of the textbook, p. 313
(this key uses 7-bit binary ASCII format)
How would we encode INFT 101 into decimal and ultimately binary?
Char. Dec
Binary
I = 73 = 1001001
N = 78 = 1001110
F = 70 = 1000110
T = 84 = 1010100
space = 32 = 0100000
1 = 49 = 0110001
0 = 48 = 0110000
1 = 49 = 0110001
INFT 101 = 10010011001110100011010101000100000011000101100000110001
18
ASCII Decoding Example
Decode this binary sequence:
01000011011010000110000101110000011101000110010101110010
One option is to break it down into bytes, translate into decimal numbers, and
look up on ASCII chart:
01000011 01101000 01100001 01110000 01110100 01100101 01110010
67
104
97
112
116
101
114
C
h
a
p
t
e
r
19
How Many Bits Are Necessary to
Represent Something?










1 bit can represent two (21) symbols

either a 0 or a 1
2 bits can represent four (22) symbols
 00 or 01 or 10 or 11
3 bits can represent eight (23) symbols
 000 or 001 or 011 or 111 or 100 or 110 or 101 or 010
4 bits can represent sixteen (24) symbols
5 bits can represent 32 (25) symbols
6 bits can represent 64 (26) symbols
7 bits can represent 128 (27) symbols
8 bits (a byte) can represent 256 (28) symbols
n bits can represent (2n) symbols!
So…how many bits are necessary for all 102 of us in class to have a
unique binary ID?
20
Question

Is 64-bit twice as big as 32-bit?




Is 8-bit twice as big as 4-bit?



32 bit = 232 = 4,294,967,296 bits
64 bit = 264 = 1.8 x 1019 bits
128 bit = 2128 = 3.4 x 1038 bits
8 bit = 28 = 256 bits
4 bit = 24 = 16 bits
Remember that we’re dealing with exponents!




8
7
7
8
bit is twice as big as __________?
bit!
bits provide 27 possible values or 2x2x2x2x2x2x2 = 128
bits provide 28 possible values or 2x2x2x2x2x2x2x2 = 256
21
Something to Remember

“Bits” are often used in terms of a data rate, or speed of
information flow:



56 Kilobit per second modem (56 Kbps)
A T-1 is 1.544 Megabits per second (1.544 Mbps or 1544 Kbps)
“Bytes” are often used in terms of storage or capacity-computer memories are organized in terms of 8 bits


256 Megabyte (MB) RAM
40 Gigabyte (GB) Hard disk
22
Note! The Multipliers for Bits and Bytes
are Slightly Different.
“Kilo” or “Mega” have slightly different values when used with bits per second
or with bytes.


When Referring to Bytes (as in computer memory)

Kilobyte (KB)
210 = 1,024 bytes

Megabyte (MB)
220 = 1,048,576 bytes

Gigabyte (GB)
230 = 1,073,741,824 bytes

Terabyte (TB)
240 = 1,099,511,627,776 bytes
When Referring to Bits Per Second (as in transmission rates)

Kilobit per second (Kbps) = 1000 bps (thousand)

Megabit per second (Mbps) = 1,000,000 bps (million)

Gigabit per second (Gbps) = 1,000,000,000 bps (billion)

Terabit per second (Tbps) = 1,000,000,000,000 bps (trillion)
23
More Multipliers for Measuring Bytes

Kilobyte (K)
210 = 1,024 bytes

Megabyte (M) 220 = 1,048,576 bytes

Gigabyte (G) 230 = 1,073,741,824 bytes

Terabytes (T) 240 = 1,099,511,627,776 bytes

Petabytes (P) 250 = 1,125,899,906,842,624 bytes

Exabytes (E) 260 = 1,152,921,504,606,846,976 bytes

Zettabytes (Z) 270 = 1,180,591,620,717,411,303,424 bytes

Yottabytes (Y) 280 = 1,208,925,819,614,629,174,706,176 bytes
24
New Topics

The Octal Numbering System:


The Hexadecimal Numbering System:


What is It and When Would We Use It?
What is It and When Would We Use It?
Converting between Binary, Octal, and Hexadecimal
25
Alternative Notations - Octal and
Hexadecimal Representation




When dealing with collections of bits - for example binary words
representing text characters using the ASCII code - it can be
inconvenient to deal with each individual bit.
Sometimes it’s necessary to examine particular bit patterns to
determine whether a system is operating properly.
For such applications, we find it easier to use a “shorthand”
notation, or alternative notation.
Two examples are “octal” and “hex”


Octal--base 8--Uses 8 Numerals

0, 1, 2, 3, 4, 5, 6, 7
Hexadecimal--base 16--Uses 16 Numerals

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
26
Octal


A counting system using the first eight numerals starting with zero.
So the first 20 numbers of this system are:


0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15, 16, 17, 20, 21, 22, 23
Because each numeral can only take on one of 8 values, and because
8 is a power of 2, we can use an octal numeral as shorthand to
represent a group of 3 bits:

Octal Numeral
0
1
2
3
4
5
6
7
Bit Pattern
000
001
010
011
100
101
110
111
27
Converting Binary to Octal



For example, the number 101001010111 may be converted to
octal form by grouping the bits into 3.
Starting from the right, break the bit pattern into groups of
three:

The first 3-bits are: 111, corresponding to 7.

The next 3-bits are: 010, corresponding to 2.

The next are 001, corresponding to 1.

and the last 3-bits are 101, corresponding to 5.
The above binary number may be represented by: 5127(8) in
octal form
28
Octal Example

If the following 12-bit pattern is stored in a computer’s memory:


010110011101(2)
We can represent these bits in octal as:

2635(8)
29
In-Class Problems:
Binary to Octal Conversion

Convert to octal:

110110101010(2)

100011010(2)
30
In-Class Problems:
Octal to Binary Conversion

Convert to binary:

63(8)

215(8)
Could we convert 291?
31
Hexadecimal

The “Hex” system uses 16 numerals:


What are the first 20 numbers of the hex system?


0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, 10, 11, 12, 13
Because each numeral can take on one of 16 values, and
because 16 is another power of two, we can use a hex numeral
to represent a grouping of four bits.
32
Comparing Decimal, Octal, Hex, Binary
Decimal
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Octal
0
1
2
3
4
5
6
7
10
11
12
13
14
15
16
17
Hex
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
Bit Pattern
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
33
Converting Binary to Hexadecimal



For example, the number 101011010111(2) may be converted to
hexadecimal form by grouping the bits into 4 and translated as
follows:
Starting from the right, count 4 digits to the left.

The first 4-bits are: 0111, corresponding to 7(16).

The next 4-bits are: 1101, corresponding to D(16).

The last bits are: 1010, corresponding to A(16)
The above binary number may be represented by: AD7(16) in
hexadecimal form
34
In-Class Problems

Convert to Hex:

101101110101(2)
35
In-Class Problems

Convert to Hex:

101110101101(2)
36
In-Class Problems

Convert to Binary:

ADD1(16)
37
Converting Octal and Hex to Decimal




To convert octal and hex to decimal, we apply the same technique as
converting binary to decimal.
Remember that we summed together the weights of the various
positions in the binary number which contained a “1” to convert from
binary to decimal.
Similarly, we sum the weights of the various positions in the octal or
hex numbers depending on the base being used.

Binary is base 2

Octal is base 8

Hex is base 16
Example of octal to decimal conversion..
38
Real World Hexadecimal Examples

Important In-Class Examples


Ethernet Addresses
Internet Addresses (IPv6)
39
Download