Binary Expression

Download Report

Transcript Binary Expression

Binary Expression
Numbers & Text
CS 105
Binary Representation
• At the fundamental hardware level, a modern
computer can only distinguish between two
values, 0 and 1.
• We have to represent the information we
want to store and manipulate in binary format.
Data Representation
•
•
•
•
•
Number
Text
Images and graphics
Audio
Video
Positional Notation
• Base
– Binary (2)
• 0, 1
– Octal (8)
• 0, 1, 2, 3, 4, 5, 6, 7
– Decimal (10)
• 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
– Hexadecimal (16)
• 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
• Positional Notation
– 943 = 9 * 100 + 4 * 10 + 3 * 1
– 63578 = ?
Binary Expression (Binary to Decimal)
• Example1: To convert the binary number
100111 to decimal, we write:
the binary string:
1 0 1 1 1
over the powers of 2: 16 8 4 2 1
16 + 4 + 2 + 1 = 23
• Like bills, no matter how much money you
want, I can always give the accurate amount
by using 1, 2, 4, 8, 16, 32, … bills
Binary Expression (Decimal to Binary)
• Example2: To convert the decimal number 23 to binary, we
do the following divisions, recording the quotients and the
remainders
1. 23 divided by 2 has a quotient of 11 and a remainder of 1.
2. 11 divided by 2 has a quotient of 5 and a remainder of 1.
3. 5 divided by 2 has a quotient of 2 and a remainder of 1.
4. 2 divided by 2 has a quotient of 1 and a remainder of 0.
5. 1 divided by 2 has a quotient of 0 and a remainder of 1.
6. The sequence of remainders we obtained, when collected
the reverse order is the binary representation of 23, namely
10111.
Binary Expression (Decimal to Binary)
• Combination of 1, 2, 4, 8, 16, 32, … bills
–
–
–
–
–
–
–
–
Decimal Number: 23
32 16 8 4 2 1
1. You have 23. Pick 16, and you have 7 left.
2. You have 7. Pick 4 (8 is too large), and you have 3 left.
3. You have 3. Pick 2, and you have 1 left.
4. You have 1. Pick 1
So, …
32 16 8 4 2 1
We have
1 0 1 1 1 (binary expression for 23)
Binary representation.
• Remember, everything on a computer is stored as 0s and 1s.
• Thus, we must interpret these numbers as different forms of data.
• One bit (binary digit) can be either a 0 or a 1.
– Therefore, it can only represent two possibilities: hot or cold, black or
white, on or off, etc…
Numerical data Operation
•
•
Multiply the binary number 101*1001.
–
–
–
–
–
–
•
•
1001
x 101
1001
0000
1001
101101
Add the binary number 100110 + 111001.
–
–
–
–
100110
+
111001
_________________
1011111
Data Representation
•
•
•
•
•
Number
Text
Images and graphics
Audio
Video
Binary representation. cont.
• If we want to represent for things, we must use more bits.
• Two bits can be either 00, 01, 10, or 11.
– Therefore, they can represent four possibilities: cold, cool, warm, or hot;
black, white, and two gray levels; etc …
• This continues until we have enough bits to represent our data.
– In general, n bits can represent 2n things (ie, every time we add a bit,
we double the number of things we can represent by two).
Some terminology.
• Up to this point we have been talking about data in either bits or bytes.
– 1 byte = 8 bits
• While this is the correct way to talk about data, sometimes it is a bit
inefficient.
• Therefore, we use prefixes to given an order of magnitude.
– Much the same way we do with the metric system.
• The following is a list of the common terms.
–
–
–
–
–
Kilobyte (KB) = 103 = 1000 bytes
Megabyte (MB) = 106 = 1 million bytes
Gigabyte (GB) = 109 = 1 billion bytes
Terabyte (TB) = 1012 = 1 trillion bytes
Petabyte (PB) 1015 = 1 quadrillion bytes
Representing text.
• Now we know how numbers are represented by computers, but
what about text, such as term papers, text messages, etc…?
• Well, we can use a technique that we’ve referenced a couple of
times. That is, we can use numbers to represent elements of a set.
–
–
–
–
How could we represent the letters ‘a’ to ‘z’?
One possibility would be to use 0 as ‘a’ and 25 as ‘z’.
Is there a problem with this?
What if we wanted to represent the ‘the ABCs and 123s’?
• We’re going to need a way to represent all letters, numbers, symbols, and
hidden characters. Remember newline?
• In fact there are many different systems used to do this, one of the
most popular is the ascii system, which is used by notepad on
windows.
– ASCII stands for American Standard Code for Information Interchange.
ASCII Character Set
Ascii codes. cont.
• An ascii character is an 8 bit (1 byte) number interpreted to represent
something.
–
–
–
–
–
48 represents ‘0’
82 represents ‘R’
32 represents ‘ ’ (Space)
Etc …
How many characters can be represented by Ascii?
• A program can therefore interpret a chain of 8 bit numbers to represent a
text document.
– Notepad in Windows does just this.
– In notepad, we can verify this by creating a new document. Typing a single
letter in it then saving. If we now check the size of the document, you’ll see
that it is 1 byte.
• Type another character and save … now it’s 2 bytes.
Representing text. cont.
• How about other characters (Latin, Russian, Thai, math symbols, Chinese,
Japanese, Korean, …)?
• There are actually many other standards for representing text on
computers aside from ASCII.
• One very common one is Unicode.
– Unicode is a standard used to represent around 100,000 different characters
covering most languages.
– In fact, in Microsoft Word, try clicking Insert  Symbol. You may notice in the
right hand corner that the set of symbols is taken from Unicode.
• You will also be shown the Hexadecimal version of the code for that character.
– Unicode uses a 16bit (2byte) or 32bit (4byte) code to represent characters
depending on the version of Unicode.
• How many characters can Unicode represent?