1 / 32

Binary numbers, bits, and Boolean operations

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)

derek-lloyd
Télécharger la présentation

Binary numbers, bits, and Boolean operations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

  3. “Bits” of information • 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.

  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 n1) + (0 x n0), 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 n1) + (0 x n0) = (1 x ten1) + (0 x ten0) = (1 x ten) + (0 x one) = ten

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

  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?

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

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

  11. 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)

  12. binary -> decimal practice • 11 • 1010 • 1000001111

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

  14. 20 = 1 21 = 2 22 = 4 23 = 8 24 = 16 25 = 32 26 = 64 27 = 128 28 = 256 29 = 512 210 = 1024 Powers of two

  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.

  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

  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

  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. 1 1 0 1

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

  20. In-class practice • 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

  22. Binary operations • 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

  24. Example 1 1 1 101011 +011010 1 0 0 0 1 0 1

  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)

  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

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

  28. Truth tables

  29. Truth tables (0 = F; 1 = T)

  30. In-class practice • (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

  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.

More Related