Download Example 1

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

Elementary mathematics wikipedia , lookup

Addition wikipedia , lookup

Location arithmetic wikipedia , lookup

Arithmetic wikipedia , lookup

Positional notation wikipedia , lookup

Transcript
Binary numbers, bits, and
Boolean operations
CSC 2001
Overview
sections 1.1, 1.5
bits
binary (base 2 numbers)
conversion to and from
addition
Boolean logic
“Bits” of information
binary digits
0 or 1
why?
not necessarily intuitive, but…
easy (on/off)
powerful (more in a later lecture)
Number bases
When we see that number 10, we
naturally assume it refers to the value
ten.
So, when we read this…
There are 10 kinds of people in this world:
those who understand binary, and those
who don't.
It might seem a little confusing.
Number bases
In the world today, pretty much everyone
assumes numbers are written in base ten.
Originated in India
This cultural norm is very useful!
But 10 does not necessarily mean ten.
What it really means is…
(1 x n1) + (0 x n0), where n is our base or our
number system.
Base ten (decimal)
So in base ten, we’ll set n = ten.
Thus…
10 =
(1 x n1) + (0 x n0) =
(1 x ten1) + (0 x ten0) =
(1 x ten) + (0 x one) =
ten
Base ten (decimal)
527 =
(5 x ten2) + (2 x ten1) + (7 x ten0) =
(5 x one hundred) + (2 x ten) + (7 x one)
=
five hundred and twenty seven
Other bases
In computer science, we’ll see that base
2, base 8, and base 16 are all useful.
Do we ever work in something other
than base 10 in our everyday life?
Other bases
Base twelve
Sumerian?
smallest number divisible by 2, 3, & 4
time, astrology/calendar, shilling, dozen/gross,
foot
10 (base twelve) = 12 (base ten) [12 + 0]
527 (base twelve) = 751 (base ten) [(5x144) +
(2x12) + (7x1)]
Other bases
Base sixty
Babylonians
smallest number divisible by 2, 3, 4, & 5
time (minutes, seconds),
latitude/longitude, angle/trigonometry
10 (base sixty) = 60 (base ten) [60 + 0]
527 (base sixty) = 18,127 (base ten)
[(5x602) + (2x60) + 7 = (5x3600) + 120 +
7]
Base two (binary)
Just like the other bases…
number abc = (a x two2) + (b x two1) + (c x
two0) = (a x 4) + (b x 2) + (c x 1)
So..
 There are 10 kinds of people in this world: those
who understand binary, and those who don't.
means there are 2 kinds of people (1x2 + 0x1)
binary -> decimal practice
11
1010
1000001111
Answers
11 =
(1x2) + (1x1) = 3
1010 =
(1x23)+(0x22)+(1x2)+(0x1) =
8 + 0 + 2 + 0 = 10
1000001111 =
(1x29) + (1x23) + (1x22) + (1x2) + (1x1) =
512 + 8 + 4 + 2 + 1 = 527
Powers of two
20
21
22
23
24
25
=
=
=
=
=
=
1
2
4
8
16
32
26 = 64
27 = 128
28 = 256
29 = 512
210 = 1024
decimal -> binary
Algorithm (p. 42) figure 1.17
Step 1: Divide the value by two and record
the remainder
Step 2: As long as the quotient obtained is
not zero, continue to divide the newest
quotient by two and record the remainder
Step 3: Now that a quotient of zero has been
obtained, the binary representation of the
original value consists of the remainders
written from right to left in the order they
were recorded.
Example 1:
13 (base ten) = ?? (base 2)
Step 1: Divide the value by two and
record the remainder
13/2 = 6 (remainder of 1)
1
Example 1:
13 (base ten) = ?? (base 2)
13/2 = 6 (remainder of 1)
1
Step 2: As long as the quotient obtained is
not zero, continue to divide the newest
quotient by two and record the remainder
6/2 = 3 (remainder of 0)
0
3/2 = 1 (remainder of 1)
1
1/2 = 0 (remainder of 1)
1
Example 1:
13 (base ten) = ?? (base 2)
13/2 = 6 (remainder of 1)
1
6/2 = 3 (remainder of 0)
0
3/2 = 1 (remainder of 1)
1
1/0 = 0 (remainder of 1)
1
Step 3: Now that a quotient of zero has been
obtained, the binary representation of the
original value consists of the remainders
written from right to left in the order they
were recorded.
1 1 0 1
Example 2: 527
527/2 = 263 r 1
263/2 = 131 r 1
131/2 = 65 r 1
65/2 = 32 r 1
32/2 = 16 r 0
16/2 = 8 r 0
8/2 = 4 r 0
4/2 = 2 r 0
2/2 = 1 r 0
1/2 = 0 r 1
1
1
1
1
0
0
0
0
0
1
1 0 0 0 0 01111
In-class practice
37
18
119
Answers
 37:
 37/2=18r1; 18/2=9r0; 9/2=4r1; 4/2=2r0; 2/2=1r0;
1/2=0r1
 100101 = 1 + 4 + 32 = 37
 18:
 18/2=9r0; 9/2=4r1; 4/2=2r0; 2/2=1r0; 1/2=0r1
 10010 = 2 + 16 = 18
 119:
 119/2=59r1; 59/2=29r1; 29/2=14r1; 14/2=7r0; 7/2=3r1;
3/2=1r1; 1/2=0r1
 1110111 = 1 + 2 + 4 + 16 + 32 + 64 = 119
Binary operations
Basic functions of a computer
Arithmetic
Logic
Binary addition
Addition
Useful binary addition facts:
0
1
0
1
+
+
+
+
0
0
1
1
=
=
=
=
0
1
1
10
Example
11 1
101011
+011010
100 01 01
Multiplication and division by 2
Multiply by 2
add a zero on the right side
1 x 10 = 10
10 x 10 = 100
Integer division by 2 (ignore remainder)
drop the rightmost digit
100/10 = 10
1000001111/10 = 100000111
(527/2 = 263)
Binary numbers & logic
As we have seen, 1’s and 0’s can be
used to represent numbers
They can also represent logical values
as well.
True/False (1/0)
George Boole
Logical operations and binary
numbers
Boolean operators
AND
OR
XOR (exclusive or)
NOT
Truth tables
AND
F
T
OR
F
T
F
F
F
F
F
T
T
F
T
T
T
T
XOR
F
T
NOT
F
T
F
F
T
-
T
F
T
T
F
-
-
-
Truth tables (0 = F; 1 = T)
AND
0
1
OR
0
1
0
0
0
0
0
1
1
0
1
1
1
1
XOR
0
1
NOT
0
1
0
0
1
-
1
0
1
1
0
-
-
-
In-class practice
(1 AND 0) OR 1
(1 XOR 0) AND (0 AND 1)
(1 OR ???)
(0 AND ???)
Answers
(1 AND 0) OR 1 =
0 OR 1 = 1
(1 XOR 0) AND (0 AND 1) =
1 AND 0 = 0
(1 OR ???) =
1
(0 AND ???) =
0
Summary
 Binary representation and arithmetic and Boolean
logic are fundamental to the way computers
operate.
 Am I constantly performing binary conversions
when I program?
 Absolutely not (actually hardly ever!)
 But understanding it makes me a better programmer.
 Am I constantly using Boolean logic when I
program?
 Definitely!
 A good foundation in logic is very helpful when working
with computers.