CMSC 100 CMSC 100 From the Bottom Up: It's All Just Bits Professor Marie desJardins Tuesday,...

28
CMSC 100 CMSC 100 From the Bottom Up: It's All Just Bits Professor Marie desJardins Tuesday, September 11, 2012 Tue 9/11/12 1 CMSC 100 -- Just Bits

Transcript of CMSC 100 CMSC 100 From the Bottom Up: It's All Just Bits Professor Marie desJardins Tuesday,...

CMSC 100CMSC 100

From the Bottom Up: It's All Just Bits

Professor Marie desJardins

Tuesday, September 11, 2012

Tue 9/11/121CMSC 100 -- Just Bits

Just BitsJust Bits Inside the computer, all information is stored as bits

A “bit” is a single unit of information Each “bit” is set to either zero or one

How do we get complex systems like Google, Matlab, and our cell phone apps?

Tue 9/11/12CMSC 100 -- Just Bits

2

Today’s TopicsToday’s Topics Logic operations and truth tables

Logic gates and simple circuits Memory storage

Binary numbers Conversions Hexadecimal

Representing other data Integers (including negative numbers) Floating point numbers Characters (ASCII)

Tue 9/11/12CMSC 100 -- Just Bits

3

Logic and Truth Tables

Tue 9/11/12CMSC 100 -- Just Bits 4

Manipulating BitsManipulating Bits What does a bit value “mean”?

0 = FALSE [off, no] 1 = TRUE [on, yes]

Just as in regular algebra, we can think of “variables” that represent a single bit If X is a Boolean variable, then the value of X is either:

0 [FALSE, off, no] or 1 [TRUE, on, yes]

In algebra, we have operations that we can perform on numbers: Unary operations: Negate, square, square-root, … Binary operations: Add, subtract, multiply, divide, …

What are the operations you can imagine performing on bits? Unary: ?? Binary: ??

Thought problem: How many unary operations are there? How many binary operations?

Tue 9/11/12CMSC 100 -- Just Bits

5

Remember Function Remember Function Tables?Tables?

Tue 9/11/12CMSC 100 -- Just Bits

6

X X2

0 0

1 1

2 4

3 9

4 16

… …

X Y X + Y

0 0 0

0 1 1

0 2 2

1 0 1

1 1 2

1 2 3

2 0 2

… … …

Boolean Operations:Boolean Operations:“Truth Tables”“Truth Tables”

Negation (NOT):

Conjunction (AND): Disjunction (OR):

Tue 9/11/12CMSC 100 -- Just Bits

7

X X

0 1

1 0

X Y X Y

0 0 0

0 1 0

1 0 0

1 1 1

X Y X Y0 0 0

0 1 1

1 0 1

1 1 1

X X

F T

T F

Or equivalently…

X Y X Y

F F F

F T F

T F F

T T T

X Y X YF F F

F T T

T F T

T T T

Boolean ExpressionsBoolean Expressions EXERCISE: What would a truth table look like for

the expression:(A B) (B C)

Tue 9/11/12CMSC 100 -- Just Bits

8

A B C A B B B C (A B) (B C)

Boolean ExpressionsBoolean Expressions What would a truth table look like for the

expression:(A B) (B C)

Tue 9/11/12CMSC 100 -- Just Bits

9

A B C A B B B C (A B) (B C)

0 0 0

0 0 1

0 1 0

0 1 1

1 0 0

1 0 1

1 1 0

1 1 1

Boolean ExpressionsBoolean Expressions What would a truth table look like for the

expression:(A B) (B C)

Tue 9/11/12CMSC 100 -- Just Bits

10

A B C A B B B C (A B) (B C)

0 0 0 0 1 1 0

0 0 1 0 1 1 0

0 1 0 1 0 0 0

0 1 1 1 0 1 1

1 0 0 1 1 1 1

1 0 1 1 1 1 1

1 1 0 1 0 0 0

1 1 1 1 0 1 1

(Boolean) Algebraic (Boolean) Algebraic LawsLaws

DeMorgan’s Theorem Analogous to the distributive law in regular algebra

(A B) = A B (A B) = A B

Tue 9/11/12CMSC 100 -- Just Bits

11

A B A B (A B) A B A B

F F F T T T T

F T F T T F T

T F F T F T T

T T T F F F F

Logic Gates, Circuits, and

Memory

Tue 9/11/12CMSC 100 -- Just Bits 12

Storing BitsStoring Bits How are these “bits” stored in the computer?

A bit is just an electrical signal or voltage(by convention: “low voltage” = 0; “high voltage” = 1)

A circuit called a “flip-flop” can store a single bit A flip-flop can be “set” (using an electrical signal) to either 0

or 1 The flip-flop will hold that value until it receives a new signal

telling it to change

Bits can be operated on using gates (which “compute” a function of two or more bits)

Tue 9/11/12CMSC 100 -- Just Bits

13

Logic GatesLogic Gates In hardware, Boolean

operations are implemented using circuits called logic gates

Tue 9/11/12CMSC 100 -- Just Bits

14

XOR:

Exclusive or

(one input is

TRUE, but not

both)

Logic CircuitsLogic Circuits We can implement any logical expression just by

assembling the associated logical gates in the right order

What would a logic circuit look like for the expression:

(A B) (B C)

Tue 9/11/12CMSC 100 -- Just Bits

15

Storing InformationStoring Information One bit can’t tell you much… (just 2 possible values)

Usually we group 8 bits together into one “byte”

QUESTION:How many possible values (combinations) are there for one byte?

A byte can just be thought of as an 8-digit binary (base 2) number Michael Littman's octupus counting video

Low-order or least significant bit == ones place Next bit would be “10s place” in base 10 -- what about base 2?

High-order bit or most significant bite in a byte == ?? place

Tue 9/11/12CMSC 100 -- Just Bits

16

Orders of MagnitudeOrders of Magnitude One 0/1 (“no/yes”) “bit” is the basic unit of memory

Eight (23) bits = one byte 1,024 (210) bytes = one kilobyte (1K)*

1,024K = 1,048,576 (220 bytes) = one megabyte (1M) 1,024K (230 bytes) = one gigabyte (1G) 1,024 (240 bytes) = one terabyte (1T) 1,024 (250 bytes) = one petabyte (1P) ... 280 bytes = one yottabyte (1Y?)

Tue 9/11/12CMSC 100 -- Just Bits

17

Scaling Up MemoryScaling Up Memory Computer chip:

Many (millions) of circuits Etched onto a silicon wafer using VLSI (Very Large-Scale Integration)

technology Lots of flip-flops or DRAM devices == memory chip

Each byte has an address (and we use binary numbers to represent those addresses…) An address is represented using a word, which is typically either;

2 bytes (16 bits) -- earliest PCs Only 64K combinations memory is limited to 64K (65,535) bytes!

4 bytes (32 bits) -- first Pentium chips This brings us up to 4G (4,294,967,295) bytes of memory!

8 bytes (64 bits) -- modern Pentium chips Up to 16.8 million terabytes (that’s 18,446,744,073,709,551,615 bytes!)

Tue 9/11/12CMSC 100 -- Just Bits

18

Binary and Hexadecimal

Numbers

Tue 9/11/12CMSC 100 -- Just Bits 19

Binary NumbersBinary Numbers A binary (base-two) number is just like a decimal (base-ten)

number, except that instead of ten possible digits (0...9), we only have two (0...1) 1510 47710

1112 1110111012

Tue 9/11/12CMSC 100 -- Just Bits

20

tens placeones place

tens placeones placehundreds place

twos place

ones placefours place

QUESTION: What place value does this zero hold?

ConversionsConversions Binary decimal: multiply each digit by its place value and add

results Decimal binary:

Find the largest power of two that is less than or equal to the decimal number

Put a one in that place column in the binary number Add spaces for the smaller binary digits E.g., if the largest multiple of two that fits is 256, you would

write: 1 __ __ __ __ __ __ __ __

Find the next smallest multiple of two Put a one in that place column (and a zero in any columns you

skipped) EXERCISE: You try it!

Write 69710 in binary

Write 110011102 in decimal Tue 9/11/12CMSC 100 -- Just Bits2

21

HexadecimalHexadecimal It would be very inconvenient to write

out a 64-bit address in binary:0010100111010110111110001001011000010001110011011110000011100000

Instead, we group each set of 4 bits together into a hexadecimal (base 16) digit: The digits are 0, 1, 2, …, 9, A (10), B (11), …, E (14), F (15)

0010 1001 1101 0110 1111 1000 1001 0110 0001 0001 1100 1101 1110 0000 1110 00002 9 D 6 F 8 9 6 1 1 C D E 0 E 0

…which we write, by convention, with a “0x” preceding the number to indicate it’s a heXadecimal number: 0x29D6F89611CDE0E0

Tue 9/11/12CMSC 100 -- Just Bits

22

Representing Representing InformationInformation

Positive integers: Just use the binary number system

Negative integers, letters, images, … not so easy! There are many different ways to represent information Some are more efficient than others

… but once we’ve solved the representation problem, we can use that information without considering how it’s represented… via

Tue 9/11/12CMSC 100 -- Just Bits

23

Representing IntegersRepresenting Integers Simplest idea (“ones’ complement”):

Use one bit for a “sign bit”: 1 means negative, 0 means positive

The other bits are “complemented” (flipped) in a negative number So, for example, +23 (in a 16-bit word) is represented as:

0000000000010111and -23 is represented as:

1111111111101000 But there are two different ways to say “zero” (0000… and 1111…) It’s tricky to do simple arithmetic operations like addition in the ones’

complement notation This is solved by the twos’ complement representation, but we

won’t go over that

Tue 9/11/12CMSC 100 -- Just Bits

24

Floating Point Floating Point NumbersNumbers

Non-integers are a problem… Remember that any rational number can be represented as a fraction

…but we probably don’t want to do this, since (a) we’d need to use two words for each number (i.e., the numerator and the

denominator) (b) fractions are hard to manipulate (add, subtract, etc.)

Irrational numbers can’t be written down at all, of course Notice that any representation we choose will by definition have limited

precision, since we can only represent 232 different values in a 32-bit word 1/3 isn’t exactly 1/3 (let’s try it on a calculator!)

In general, we also lose precision (introduce errors) when we operate on floating point numbers

You don’t need to know the details of how “floating point” numbers are represented

Tue 9/11/12CMSC 100 -- Just Bits

25

Representing Representing CharactersCharacters

ASCII representation: one byte [actually 7 bits…] == one letter == an integer from 0-128

No specific reason for this assignment of letters to integers!

UNICODE is a popular 16-bit representation that supports accented characters like é

Tue 9/11/12CMSC 100 -- Just Bits

26 [Chart borrowed from ha.ckers.org]

Summary: Main IdeasSummary: Main Ideas It’s all just bits Abstraction Boolean algebra

TRUE/FALSE Truth tables Logic gates

Representing numbers Hexadecimal representation Ones’ complement Floating point numbers (main issues)

ASCII representation (main idea)

Tue 9/11/12CMSC 100 -- Just Bits

27

ACTIVITY (if time)ACTIVITY (if time) Design a one-bit adder (i.e., a logic circuit that adds two

1-bit numbers together) X + Y Z2 Z1

Z2 is needed since the result may be two binary digits long

First let’s figure out the Boolean expression for each output… Create the truth table

Then we’ll draw the logic circuit

Tue 9/11/12CMSC 100 -- Just Bits

28