Top Banner
Binary numbers, bits, and Boolean operations CSC 2001
32

Binary numbers, bits, and Boolean operations

Dec 31, 2015

Download

Documents

derek-lloyd

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) - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Binary numbers, bits, and Boolean operations

Binary numbers, bits, and Boolean operations

Binary numbers, bits, and Boolean operations

CSC 2001CSC 2001

Page 2: Binary numbers, bits, and Boolean operations

OverviewOverview

sections 1.1, 1.5bitsbinary (base 2 numbers)

conversion to and fromaddition

Boolean logic

sections 1.1, 1.5bitsbinary (base 2 numbers)

conversion to and fromaddition

Boolean logic

Page 3: Binary numbers, bits, and Boolean operations

“Bits” of information“Bits” of information

binary digits0 or 1

why?not necessarily intuitive, but…easy (on/off)powerful (more in a later lecture)

binary digits0 or 1

why?not necessarily intuitive, but…easy (on/off)powerful (more in a later lecture)

Page 4: Binary numbers, bits, and Boolean operations

Number basesNumber 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.

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.

Page 5: Binary numbers, bits, and Boolean operations

Number basesNumber bases

In 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.

In 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.

Page 6: Binary numbers, bits, and Boolean operations

Base ten (decimal)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

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

Page 7: Binary numbers, bits, and Boolean operations

Base ten (decimal)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

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

Page 8: Binary numbers, bits, and Boolean operations

Other basesOther 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?

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?

Page 9: Binary numbers, bits, and Boolean operations

Other basesOther 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)]

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)]

Page 10: Binary numbers, bits, and Boolean operations

Other basesOther 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 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]

Page 11: Binary numbers, bits, and Boolean operations

Base two (binary)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)

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)

Page 12: Binary numbers, bits, and Boolean operations

binary -> decimal practicebinary -> decimal practice

11

1010

1000001111

11

1010

1000001111

Page 13: Binary numbers, bits, and Boolean operations

AnswersAnswers

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

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

Page 14: Binary numbers, bits, and Boolean operations

Powers of twoPowers of two

20 = 121 = 222 = 423 = 824 = 1625 = 32

20 = 121 = 222 = 423 = 824 = 1625 = 32

26 = 6427 = 12828 = 25629 = 512210 = 1024

26 = 6427 = 12828 = 25629 = 512210 = 1024

Page 15: Binary numbers, bits, and Boolean operations

decimal -> binarydecimal -> binary

Algorithm (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.

Algorithm (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.

Page 16: Binary numbers, bits, and Boolean operations

Example 1:Example 1:

13 (base ten) = ?? (base 2)Step 1: Divide the value by two

and record the remainder13/2 = 6 (remainder of 1)

1

13 (base ten) = ?? (base 2)Step 1: Divide the value by two

and record the remainder13/2 = 6 (remainder of 1)

1

Page 17: Binary numbers, bits, and Boolean operations

Example 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

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

Page 18: Binary numbers, bits, and Boolean operations

Example 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.

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

Page 19: Binary numbers, bits, and Boolean operations

Example 2: 527Example 2: 527

527/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

527/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

Page 20: Binary numbers, bits, and Boolean operations

In-class practiceIn-class practice

37

18

119

37

18

119

Page 21: Binary numbers, bits, and Boolean operations

AnswersAnswers

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

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

Page 22: Binary numbers, bits, and Boolean operations

Binary operationsBinary operations

Basic functions of a computerArithmeticLogic

Basic functions of a computerArithmeticLogic

Page 23: Binary numbers, bits, and Boolean operations

Binary additionBinary addition

AdditionUseful binary addition facts:

0 + 0 = 01 + 0 = 10 + 1 = 11 + 1 = 10

AdditionUseful binary addition facts:

0 + 0 = 01 + 0 = 10 + 1 = 11 + 1 = 10

Page 24: Binary numbers, bits, and Boolean operations

ExampleExample

101011+011010 101011+011010

10

1

10

1

0

1

01

Page 25: Binary numbers, bits, and Boolean operations

Multiplication and division by 2

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)

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)

Page 26: Binary numbers, bits, and Boolean operations

Binary numbers & logicBinary 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

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

Page 27: Binary numbers, bits, and Boolean operations

Logical operations and binary numbers

Logical operations and binary numbers

Boolean operatorsANDORXOR (exclusive or)NOT

Boolean operatorsANDORXOR (exclusive or)NOT

Page 28: Binary numbers, bits, and Boolean operations

Truth tablesTruth 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

- - -

Page 29: Binary numbers, bits, and Boolean operations

Truth tables (0 = F; 1 = T)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

- - -

Page 30: Binary numbers, bits, and Boolean operations

In-class practiceIn-class practice

(1 AND 0) OR 1

(1 XOR 0) AND (0 AND 1)

(1 OR ???)

(0 AND ???)

(1 AND 0) OR 1

(1 XOR 0) AND (0 AND 1)

(1 OR ???)

(0 AND ???)

Page 31: Binary numbers, bits, and Boolean operations

AnswersAnswers

(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

(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

Page 32: Binary numbers, bits, and Boolean operations

SummarySummary

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.

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.