Download Document

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Location arithmetic wikipedia , lookup

Arithmetic wikipedia , lookup

Addition wikipedia , lookup

Elementary mathematics wikipedia , lookup

Approximations of π wikipedia , lookup

Positional notation wikipedia , lookup

Transcript
Binary Numbers
1
Main Memory
•
•
•
•
•
•
Main memory holds information such as computer
programs, numeric data, or documents created by a word
processor.
Main memory is made up of capacitors.
If a capacitor is charged, then its state is said to be 1, or
ON.
We could also say the bit is set.
If a capacitor does not have a charge, then its state is said
to be 0, or OFF.
We could also say that the bit is reset or cleared.
2
Main Memory (con’t)
•
•
•
•
Memory is divided into cells, where each cell
contains 8 bits (a 1 or a 0). Eight bits is called a
byte.
Each of these cells is uniquely numbered.
The number associated with a cell is known as its
address.
Main memory is volatile storage. That is, if
power is lost, the information in main memory is
lost.
3
Main Memory (con’t)
• Other computer components can
get the information held at a particular address
in memory, known as a READ,
o or store information at a particular address in
memory, known as a WRITE.
• Writing to a memory location alters its contents.
• Reading from a memory location does not alter
its contents.
o
4
Main Memory (con’t)
•
•
•
•
All addresses in memory can be accessed in the
same amount of time.
We do not have to start at address 0 and read
everything until we get to the address we really
want (sequential access).
We can go directly to the address we want and
access the data (direct or random access).
That is why we call main memory RAM (Random
Access Memory).
5
Secondary Storage Media






Disks -- floppy, hard, removable (random access)
Tapes (sequential access)
CDs (random access)
DVDs (random access)
Secondary storage media store files that contain
 computer programs
 data
 other types of information
This type of storage is called persistent (permanent)
storage because it is non-volatile.
6
I/O (Input/Output) Devices



Information input and output is handled by I/O
(input/output) devices.
More generally, these devices are known as peripheral
devices.
Examples:
 monitor
 keyboard
 mouse
 disk drive (floppy, hard, removable)
 CD or DVD drive
 printer
 scanner
7
Bits, Bytes, and Words






A bit is a single binary digit (a 1 or 0).
A byte is 8 bits
A word is 32 bits or 4 bytes
Long word = 8 bytes = 64 bits
Quad word = 16 bytes = 128 bits
Programming languages use these standard number
of bits when organizing data storage and access.
8
Number Systems


The on and off states of the capacitors in
RAM can be thought of as the values 1 and 0,
respectively.
Therefore, thinking about how information is
stored in RAM requires knowledge of the
binary (base 2) number system.

Let’s review the decimal (base 10) number
system first.
9
The Decimal Number System
• The decimal number system is a positional number system.
• Example:
5 6 2 1
103 102 101 100
10
1 X 100 =
1
2 X 101 = 20
6 X 102 = 600
5 X 103 = 5000
The Decimal Number System
(con’t)
• The decimal number system is also known as base 10.
The values of the positions are calculated by taking 10
to some power.
• Why is the base 10 for decimal numbers?
o Because we use 10 digits, the digits 0 through 9.
11
The Binary Number System
• The binary number system is also known as base 2.
The values of the positions are calculated by taking 2
to some power.
• Why is the base 2 for binary numbers?
o Because we use 2 digits, the digits 0 and 1.
12
The Binary Number System
(con’t)
• The binary number system is also a positional
numbering system.
• Instead of using ten digits, 0 - 9, the binary system
uses only two digits, 0 and 1.
• Example of a binary number and the values of the
positions:
1 0 0 1 1 0 1
26 25 24 23 22 21 20
13
Converting from Binary to
Decimal
1 0 0 1 1 0 1
26 25 24 23 22 21 20
20
21
22
23
24
25
26
=1
=2
=4
=8
= 16
= 32
= 64
14
1 X 20 = 1
0 X 21 = 0
1 X 22 = 4
1 X 23 = 8
0 X 24 = 0
0 X 25 = 0
1 X 26 = 64
7710
Converting from Binary to Decimal (con’t)
Practice conversions:
Binary
11101
1010101
100111
15
Decimal
Converting From Decimal to Binary (con’t)
•
•
•
•
Make a list of the binary place values up to the number being converted.
Perform successive divisions by 2, placing the remainder of 0 or 1 in each of the
positions from right to left.
Continue until the quotient is zero.
Example:
4210
25 24 23 2 2 21 20
32 16 8 4 2 1
1
0 1 0 1 0
42/2 = 21
21/2 = 10
10/2 = 5
5/2 = 2
2/2 = 1
1/2 = 0
4210 =
R=0
R=1
R=0
R=1
R=0
R=1
1010102
16
Converting From Decimal to Binary (con’t)
Practice conversions:
Decimal
59
82
175
17
Binary
Working with Large Numbers
0101000010100111 = ?
• Humans can’t work well with binary numbers; there are
too many digits to deal with.
• Memory addresses and other data can be quite large.
Therefore, we sometimes use the hexadecimal number
system.
18
The Hexadecimal Number
System


19
The hexadecimal number system is also known as base 16.
The values of the positions are calculated by taking 16 to some
power.
Why is the base 16 for hexadecimal numbers ?
 Because we use 16 symbols, the digits 0 and 1 and the
letters A through F.
The Hexadecimal Number System (con’t)
Binary
Decimal
Hexadecimal
Binary
Decimal
Hexadecimal
0
0
0
1010
10
A
1
10
11
100
101
110
111
1000
1001
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
1011
1100
1101
1110
1111
11
12
13
14
15
B
C
D
E
F
20
The Hexadecimal Number System
(con’t)

Example of a hexadecimal number and
the values of the positions:
3 C 8 B 0 5 1
166 165 164 163 162 161 160
21
Example of Equivalent
Numbers
Binary: 1 0 1 0 0 0 0 1 0 1 0 0 1 1 12
Decimal: 2064710
Hexadecimal: 50A716
Notice how the number of digits gets
smaller as the base increases.
22
Goals of Today’s Lecture



Binary numbers
 Why binary?
 Converting base 10 to base 2
 Octal and hexadecimal
Integers
 Unsigned integers
 Integer addition
 Signed integers
C bit operators
 And, or, not, and xor
 Shift-left and shift-right
 Function for counting the number of 1 bits
 Function for XOR encryption of a message
23
Why Bits (Binary Digits)?



Computers are built using digital circuits

Inputs and outputs can have only two values

True (high voltage) or false (low voltage)

Represented as 1 and 0
Can represent many kinds of information

Boolean (true or false)

Numbers (23, 79, …)

Characters (‘a’, ‘z’, …)

Pixels

Sound
Can manipulate in many ways

Read and write

Logical operations

Arithmetic

…
24
Base 10 and Base 2


Base 10

Each digit represents a power of 10

4173 = 4 x 103 + 1 x 102 + 7 x 101 + 3 x 100
Base 2

Each bit represents a power of 2

10110 = 1 x 24 + 0 x 23 + 1 x 22 + 1 x 21 + 0 x 20 = 22
Divide repeatedly by 2 and keep remainders





12/2 = 6
6/2 = 3
3/2 = 1
1/2 = 0
Result =
R=0
R=0
R=1
R=1
1100
25
Writing Bits is Tedious for
People


Octal (base 8)
 Digits 0, 1, …, 7
 In C: 00, 01, …, 07
Hexadecimal (base 16)
 Digits 0, 1, …, 9, A, B, C, D, E, F
 In C: 0x0, 0x1, …, 0xf
0000 = 0
0001 = 1
0010 = 2
0011 = 3
0100 = 4
0101 = 5
0110 = 6
0111 = 7
1000 = 8
1001 = 9
1010 = A
1011 = B
1100 = C
1101 = D
1110 = E
1111 = F
Thus the 16-bit binary number
1011 0010 1010 1001
converted to hex is
B2A9
26
Representing Colors: RGB




Three primary colors

Red

Green

Blue
Strength

8-bit number for each color (e.g., two hex digits)

So, 24 bits to specify a color
In HTML, on the course Web page

Red: <font color="#FF0000"><i>Symbol Table Assignment
Due</i>

Blue: <font color="#0000FF"><i>Fall Recess</i></font>
Same thing in digital cameras

Each pixel is a mixture of red, green, and blue
27
Storing Integers on the
Computer



Fixed number of bits in memory

Short: usually 16 bits

Int: 16 or 32 bits

Long: 32 bits
Unsigned integer

No sign bit

Always positive or 0

All arithmetic is modulo 2n
Example of unsigned int

00000001  1

00001111  15

00010000  16

00100001  33

11111111  255
28
Adding Two Integers: Base 10


From right to left, we add each pair of digits
We write the sum, and add the carry to the next
column
0
1
1
+
0
0
1
2
Sum
1
0
0
1
Carry
0
1
1
1
9
8
+
2
6
4
Sum
4
6
Carry
0
1
29
Binary Sums and Carries
a
0
0
1
1
b
0
1
0
1
Sum
0
1
1
0
a
0
0
1
1
XOR
b
0
1
0
1
Carry
0
0
0
1
AND
0100 0101
+ 0110 0111
69
103
1010 1100
172
30
Fractional Numbers
Examples:456.7810 = 4 x 102 + 5 x 101 + 6 x 100 + 7 x 10-1+8 x 10-2
1011.112 = 1 x 23 + 0 x 22 + 1 x 21 + 1 x 20 + 1 x 2-1 + 1 x 2-2
= 8
+ 0
+ 2
+ 1
+ 1/2 + ¼
= 11 + 0.5 + 0.25 = 11.7510

Conversion from binary number system to
decimal system
Examples: 111.112 = 1 x 22 + 1 x 21 + 1 x 20 + 1 x 2-1 + 1 x 2-2
=4
+ 2
+ 1
+ 1/2 + ¼ = 7.7510
2
1
0
-1
-2
-3
Examples: 11.0112
22
21
20
2
1
2-1
2-2
2-
3
4
½
¼
1/8
31
Fractional numbers
Examples:
1.
2.
7.7510 = (?)2
Conversion of the integer part: same as before – repeated division by 2
7 / 2 = 3 (Q), 1 (R)  3 / 2 = 1 (Q), 1 (R)  1 / 2 = 0 (Q), 1 (R)
710 = 1112
Conversion of the fractional part: perform a repeated multiplication by 2 and
extract the integer part of the result
0.75 x 2 =1.50  extract 1
0.5 x 2 = 1.0  extract 1
0.7510 = 0.112
write in the same order
0.0
 stop

Combine the results from integer and fractional part, 7.7510 = 111.112
How about choose some of
4
2
1
Examples: try 5.625
1/2
1/4
=0.5
=0.25
1/8
=0.125
32
Fractional Numbers
(cont.)
Exercise 1: Convert (0.625)10 to its binary form
Solution:
0.625 x 2 = 1.25
 extract 1
0.25 x 2 = 0.5
 extract 0
0.5 x 2 = 1.0
 extract 1
0.0
 stop
 (0.625)10 = (0.101)2
Exercise 2: Convert (0.6)10 to its binary form
Solution:
0.6 x 2 = 1.2
 extract 1
0.2 x 2 = 0.4
 extract 0
0.4 x 2 = 0.8
 extract 0
0.8 x 2 = 1.6
 extract 1
0.6 x 2 =

 (0.6)10 = (0.1001 1001 1001 …)2
33
Fractional Numbers
(cont.)
Exercise 3: Convert (0.8125)10 to its
binary form
Solution:
0.8125 x 2 = 1.625  extract 1
0.625 x 2 = 1.25
 extract 1
0.25 x 2 = 0.5
 extract 0
0.5 x 2 = 1.0
 extract 1
0.0
 stop
 (0.8125)10 = (0.1101)2
34
Fractional Numbers

(cont.)
Errors

One source of error in the computations is due to back and
forth conversions between decimal and binary formats
Example: (0.6)10 + (0.6)10 = 1.210
Since (0.6)10 = (0.1001 1001 1001 …)2
Lets assume a 8-bit representation: (0.6)10 = (0 .1001 1001)2 , therefore
0.6
0.10011001
+ 0.6
 + 0.10011001
1.00110010
Lets reconvert to decimal system:
(1.00110010)b= 1 x 20 + 0 x 2-1 + 0 x 2-2 + 1 x 2-3 + 1 x 2-4 + 0 x 2-5 + 0 x 2-6 + 1 x 2-7 + 0 x 2-8
= 1 + 1/8 + 1/16 + 1/128 = 1.1953125
 Error = 1.2 – 1.1953125
= 0.0046875
35
One’s and Two’s Complement


One’s complement: flip every bit
 E.g., b 01000101 (i.e., 69 in base 10)
 One’s complement is 10111010
 That’s simply 255-69
Subtracting from 11111111 is easy (no carry needed!)
1111 1111
- 0100 0101
1011 1010

b
one’s complement
Two’s complement
 Add 1 to the one’s complement
 E.g., (255 – 69) + 1  1011 1011
36
Putting it All Together


Computing “a – b” for unsigned integers

Same as “a + 256 – b”

Same as “a + (255 – b) + 1”

Same as “a + onecomplement(b) + 1”

Same as “a + twocomplement(b)”
Example: 172 – 69

The original number 69: 0100 0101

One’s complement of 69: 1011 1010

Two’s complement of 69: 1011 1011

Add to the number 172: 1010 1100

The sum comes to:
0110 0111

Equals: 103 in base 10
1010 1100
+ 1011 1011
1 0110 0111
37
Signed Integers


Sign-magnitude representation

Use one bit to store the sign
 Zero for positive number
 One for negative number

Examples
 E.g., 0010 1100  44
 E.g., 1010 1100  -44

Hard to do arithmetic this way, so it is rarely used
Complement representation

One’s complement
 Flip every bit
 E.g., 1101 0011  -44

Two’s complement
 Flip every bit, then add 1
 E.g., 1101 0100  -44
38
Overflow: Running Out of
Room

Adding two large integers together

Sum might be too large to store in the number of bits allowed

What happens?
39
Bitwise Operators: AND and OR

Bitwise AND (&)

Bitwise OR (|)
& 0 1
0 0 0
|
0
1
0
0
1
1
1
1
1

0 1
Mod on the cheap!
 E.g., h = 53 & 15;
53 0 0 1 1 0 1 0 1
15 0 0 0 0 1 1 1 1
5
0 0 0 0 0 1 0 1
40
Bitwise Operators: Shift Left/Right

Shift left (<<): Multiply by powers of 2

Shift some # of bits to the left, filling the blanks with 0
53 0 0 1 1 0 1 0 0
53<<2

1 1 0 1 0 0 0 0
Shift right (>>): Divide by powers of 2

Shift some # of bits to the right


For unsigned integer, fill in blanks with 0
What about signed integers? Varies across machines…

Can vary from one machine to another!
53 0 0 1 1 0 1 0 0
53>>2
0 0 0 0 1 1 0 1
41
XOR Encryption



Program to encrypt text with a key

Input: original text in stdin

Output: encrypted text in stdout
Use the same program to decrypt text with a key

Input: encrypted text in stdin

Output: original text in stdout
Basic idea

Start with a key, some 8-bit number (e.g., 0110 0111)

Do an operation that can be inverted
 E.g., XOR each character with the 8-bit number
0100 0101
^ 0110 0111
0010 0010
^ 0110 0111
0010 0010
0100 0101
42
Conclusions

Computer represents everything in binary



Binary arithmetic through logic operations



Integers, floating-point numbers, characters,
addresses, …
Pixels, sounds, colors, etc.
Sum (XOR) and Carry (AND)
Two’s complement for subtraction
Binary operations in C



AND, OR, NOT, and XOR
Shift left and shift right
Useful for efficient and concise code, though
sometimes cryptic
43