binary numbers, bits, and boolean operations csc 2001
DESCRIPTION
“Bits” of information binary digits 0 or 1 why? not necessarily intuitive, but… easy (on/off) powerful (more in a later lecture) binary digits 0 or 1 why? not necessarily intuitive, but… easy (on/off) powerful (more in a later lecture)TRANSCRIPT
Binary numbers, bits, and Boolean operations
CSC 2001
Overviewsections 1.1, 1.5bitsbinary (base 2 numbers)
conversion to and fromaddition
Boolean logic
“Bits” of informationbinary digits
0 or 1why?
not necessarily intuitive, but…easy (on/off)powerful (more in a later lecture)
Number basesWhen 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 basesIn the world today, pretty much
everyone assumes numbers are written in base ten.Originated in IndiaThis 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 basesIn 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 twelveSumerian?smallest number divisible by 2, 3, & 4time, astrology/calendar, shilling,
dozen/gross, foot10 (base twelve) = 12 (base ten) [12 + 0]527 (base twelve) = 751 (base ten) [(5x144)
+ (2x12) + (7x1)]
Other bases
Base sixtyBabylonianssmallest number divisible by 2, 3, 4, & 5time (minutes, seconds),
latitude/longitude, angle/trigonometry10 (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 practice11
1010
1000001111
Answers11 =
(1x2) + (1x1) = 31010 =
(1x23)+(0x22)+(1x2)+(0x1) =8 + 0 + 2 + 0 = 10
1000001111 =(1x29) + (1x23) + (1x22) + (1x2) + (1x1) =512 + 8 + 4 + 2 + 1 = 527
Powers of two20 = 121 = 222 = 423 = 824 = 1625 = 32
26 = 6427 = 12828 = 25629 = 512210 = 1024
decimal -> binaryAlgorithm (p. 42) figure 1.17Step 1: Divide the value by two and record
the remainderStep 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 remainder13/2 = 6 (remainder of 1)
1
Example 1:13 (base ten) = ?? (base 2)13/2 = 6 (remainder of 1) 1Step 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) 03/2 = 1 (remainder of 1) 11/2 = 0 (remainder of 1) 1
Example 1:13 (base ten) = ?? (base 2)13/2 = 6 (remainder of 1) 16/2 = 3 (remainder of 0) 03/2 = 1 (remainder of 1) 11/0 = 0 (remainder of 1) 1Step 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.
101 1
Example 2: 527527/2 = 263 r 1 1263/2 = 131 r 1 1131/2 = 65 r 1 165/2 = 32 r 1 132/2 = 16 r 0 016/2 = 8 r 0 08/2 = 4 r 0 04/2 = 2 r 0 02/2 = 1 r 0 01/2 = 0 r 1 1
0 11 11 10 0 0 0
In-class practice37
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 operationsBasic functions of a computer
ArithmeticLogic
Binary additionAddition
Useful binary addition facts:0 + 0 = 01 + 0 = 10 + 1 = 11 + 1 = 10
Example
101011+011010
10
1
10
1
0
1
01
Multiplication and division by 2
Multiply by 2add a zero on the right side1 x 10 = 1010 x 10 = 100
Integer division by 2 (ignore remainder)drop the rightmost digit100/10 = 101000001111/10 = 100000111
(527/2 = 263)
Binary numbers & logicAs we have seen, 1’s and 0’s can
be used to represent numbersThey can also represent logical
values as well.True/False (1/0)George Boole
Logical operations and binary numbers
Boolean operatorsANDORXOR (exclusive or)NOT
Truth tablesAND F T
F F F
T F T
XOR F T
F F T
T T F
OR F T
F F T
T T T
NOT F T
- T F
- - -
Truth tables (0 = F; 1 = T)AND 0 1
0 0 0
1 0 1
XOR 0 1
0 0 1
1 1 0
OR 0 1
0 0 1
1 1 1
NOT 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.