Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary numbers, bits, and Boolean operations CSC 2001.

Similar presentations


Presentation on theme: "Binary numbers, bits, and Boolean operations CSC 2001."— Presentation transcript:

1 Binary numbers, bits, and Boolean operations CSC 2001

2 Overview  sections 1.1, 1.5  bits  binary (base 2 numbers)  conversion to and from  addition  Boolean logic  sections 1.1, 1.5  bits  binary (base 2 numbers)  conversion to and from  addition  Boolean logic

3 “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)

4 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.  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.

5 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 n 1 ) + (0 x n 0 ), 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 India  This cultural norm is very useful!  But 10 does not necessarily mean ten.  What it really means is…  (1 x n 1 ) + (0 x n 0 ), where n is our base or our number system.

6 Base ten (decimal)  So in base ten, we’ll set n = ten.  Thus… 10 = (1 x n 1 ) + (0 x n 0 ) = (1 x ten 1 ) + (0 x ten 0 ) = (1 x ten) + (0 x one) = ten  So in base ten, we’ll set n = ten.  Thus… 10 = (1 x n 1 ) + (0 x n 0 ) = (1 x ten 1 ) + (0 x ten 0 ) = (1 x ten) + (0 x one) = ten

7 Base ten (decimal) 527 = (5 x ten 2 ) + (2 x ten 1 ) + (7 x ten 0 ) = (5 x one hundred) + (2 x ten) + (7 x one) = five hundred and twenty seven 527 = (5 x ten 2 ) + (2 x ten 1 ) + (7 x ten 0 ) = (5 x one hundred) + (2 x ten) + (7 x one) = five hundred and twenty seven

8 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?  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?

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

10 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) [(5x60 2 ) + (2x60) + 7 = (5x3600) + 120 + 7]  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) [(5x60 2 ) + (2x60) + 7 = (5x3600) + 120 + 7]

11 Base two (binary)  Just like the other bases…  number abc = (a x two 2 ) + (b x two 1 ) + (c x two 0 ) = (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 two 2 ) + (b x two 1 ) + (c x two 0 ) = (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)

12 binary -> decimal practice  11  1010  1000001111  11  1010  1000001111

13 Answers  11 = (1x2) + (1x1) = 3  1010 = (1x2 3 )+(0x2 2 )+(1x2)+(0x1) = 8 + 0 + 2 + 0 = 10  1000001111 = (1x2 9 ) + (1x2 3 ) + (1x2 2 ) + (1x2) + (1x1) = 512 + 8 + 4 + 2 + 1 = 527  11 = (1x2) + (1x1) = 3  1010 = (1x2 3 )+(0x2 2 )+(1x2)+(0x1) = 8 + 0 + 2 + 0 = 10  1000001111 = (1x2 9 ) + (1x2 3 ) + (1x2 2 ) + (1x2) + (1x1) = 512 + 8 + 4 + 2 + 1 = 527

14 Powers of two  2 0 = 1  2 1 = 2  2 2 = 4  2 3 = 8  2 4 = 16  2 5 = 32  2 0 = 1  2 1 = 2  2 2 = 4  2 3 = 8  2 4 = 16  2 5 = 32  2 6 = 64  2 7 = 128  2 8 = 256  2 9 = 512  2 10 = 1024

15 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.  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.

16 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  13 (base ten) = ?? (base 2)  Step 1: Divide the value by two and record the remainder 13/2 = 6 (remainder of 1)1

17 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  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

18 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.  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. 1011

19 Example 2: 527 527/2 = 263 r 11 263/2 = 131 r 11 131/2 = 65 r 11 65/2 = 32 r 11 32/2 = 16 r 00 16/2 = 8 r 00 8/2 = 4 r 00 4/2 = 2 r 00 2/2 = 1 r 00 1/2 = 0 r 11 527/2 = 263 r 11 263/2 = 131 r 11 131/2 = 65 r 11 65/2 = 32 r 11 32/2 = 16 r 00 16/2 = 8 r 00 8/2 = 4 r 00 4/2 = 2 r 00 2/2 = 1 r 00 1/2 = 0 r 11 0111110000

20 In-class practice  37  18  119  37  18  119

21 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  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

22 Binary operations  Basic functions of a computer  Arithmetic  Logic  Basic functions of a computer  Arithmetic  Logic

23 Binary addition  Addition  Useful binary addition facts:  0 + 0 = 0  1 + 0 = 1  0 + 1 = 1  1 + 1 = 10  Addition  Useful binary addition facts:  0 + 0 = 0  1 + 0 = 1  0 + 1 = 1  1 + 1 = 10

24 Example 101011 +011010 101011 +011010 10 1 10 1 0 1 01

25 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)  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)

26 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  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

27 Logical operations and binary numbers  Boolean operators  AND  OR  XOR (exclusive or)  NOT  Boolean operators  AND  OR  XOR (exclusive or)  NOT

28 Truth tables ANDFT FFF TFT XORFT FFT TTF ORFT FFT TTT NOTFT -TF ---

29 Truth tables (0 = F; 1 = T) AND01 000 101 XOR01 001 110 OR01 001 111 NOT01 -10 ---

30 In-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 ???)

31 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  (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

32 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.  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.


Download ppt "Binary numbers, bits, and Boolean operations CSC 2001."

Similar presentations


Ads by Google