Lecture 2: Representing Numbers CS 2011 Fall 2014, Dr. Rozier
LADIES AND TIGERS
The Lady and the Tiger Two doors containing either Ladies or Tigers
The Lady and the Tiger You will be shown two doors, to two rooms. – Each could contain either a lady or a tiger… – It could be that both rooms contain a lady, or that both rooms contain a tiger! You will need to reason carefully and logically to survive! Each question, pick a door, or decide not to open a door. – You score one point for picking a lady, or for refusing to pick if both doors contain tigers. – Three points available for your homework/projects grade today – If you answer wrong, you may write a short paper describing what you did wrong, and how to find the right answer, due next class.
The Lady and the Tiger Form up into groups On a sheet of paper, list the first and last names of each student in the group, and pick a team name – Discuss your answers, and record them – Each group will then give their answers to the class
The Lady and the Tiger Q1 One of these is true… In this room, there is a lady, and in the other room there is a tiger. The other is false… In one of these rooms there is a lady, and in one of these rooms there is a tiger.
The Lady and the Tiger Q1 One of these is true…The other is false…
The Lady and the Tiger Q2 Either both signs are false… At least one of these rooms contains a lady Or both are true… A tiger is in the other room…
The Lady and the Tiger Q2 Either both signs are false…Or both are true…
The Lady and the Tiger Q3 Either both signs are false… Either a tiger is in this room, or a lady is in the other room. Or both are true… An lady is in the other room.
The Lady and the Tiger Q3 Either both signs are false…Or both are true…
What does this have to do with CS?
CS and CE What are the disciplines? – Computer Engineering? – Computer Science?
What it isn’t "What would we like our children- the general public of the future—to learn about computer science in schools? We need to do away with the myth that computer science is about computers. Computer science is no more about computers than astronomy is about telescopes, biology is about microscopes or chemistry is about beakers and test tubes. Science is not about tools, it is about how we use them and what we find out when we do." -- Ian Parberry
What it isn’t A confusion of even longer standing came from the fact that the unprepared included the electronic engineers that were supposed to design, build, and maintain the machines. The job was actually beyond the electronic technology of the day, and, as a result, the question of how to get and keep the physical equipment more or less in working condition became in the early days the all-overriding concern. As a result, the topic became —primarily in the USA— prematurely known as "computer science" —which, actually is like referring to surgery as "knife science"— and it was firmly implanted in people's minds that computing science is about machines and their peripheral equipment. -- Edsger Dijkstra
What it really is Computer science is the study of the theoretical foundations of information and computation and of practical techniques for their implementation and application in computer systems. Computer scientists invent algorithmic processes that create, describe, and transform information and formulate suitable abstractions to model complex systems. Computer engineering is the process of analyzing, designing, and integrating the hardware and software systems needed for information processing or computation. Computer engineers are saddled with the difficult tasks of modeling, designing, and analyzing cyberphysical systems which solve interdisciplinary problems in a wide variety of domains.
REPRESENTING NUMBERS
How can we represent numbers in a computer? What limitations do we face? What requirements do we have?
Networking and Communication What if we encode the signal into pulses? Detect if the value is above or below some threshold, and decide it represents a 1, or a 0. Strings of 1’s and 0’s can be interpreted as a number.
Some simple things we can represent with 1’s and 0’s True or false… – 1 – true – 0 – false – We already were doing this with pure signals.
Some simple things we can represent with 1’s and 0’s Integers Examples – – – – – 10 – – – 147
Unsigned Binary Integers Given an n-bit number Range: 0 to +2 n – 1 Example = 0 + … + 1× ×2 2 +1×2 1 +1×2 0 = 0 + … = Using 32 bits 0 to +4,294,967,295
Binary and Hexadecimal Because binary numbers are rather unwieldy, programmers prefer to use a more compact way to represent them. Historically, octal (base 8) and hexadecimal (base 16, or hex) have been used. – Octal has the advantage that it uses only familiar digits, but it groups digits by threes, which is inconvenient for word sizes that are multiples of 4. So it is seldom used nowadays. – Hexadecimal works nicely for bytes and for word sizes that are multiples of 4, but requires the introduction of 6 new digits. Conversion between binary and decimal is slow and is preferably avoided if there is no need. Octal and hex allow immediate conversion to and from binary.
Hexadecimal Base 16 – Compact representation of bit strings – 4 bits per hex digit c d a1010e b1011f1111 Example: eca
COMPUTING SYSTEMS
Computing Introduction to Computing Systems What is a computing system?
Computing Introduction to Computing Systems What is a computing system? What does it mean to compute something?
Chomsky’s Hierarchy What does Noam Chomsky have to do with computation?
Chomsky’s Hierarchy What does Noam Chomsky have to do with computation? What do languages have to do with computing?
Chomsky’s Hierarchy
Finite Automata
Pushdown Automata
Turing Machine
Turing Equivalence What does it take to create a Turing Machine? What does this say about computing systems?
Bits, Bytes, and Ints
Binary Representations 0.0V 0.5V 2.8V 3.3V 010
Encoding Byte Values Byte = 8 bits – Binary to – Decimal: 0 10 to – Hexadecimal to FF 16 Base 16 number representation Use characters ‘0’ to ‘9’ and ‘A’ to ‘F’ Write FA1D37B 16 in C as – 0xFA1D37B – 0xfa1d37b A B C D E F Hex Decimal Binary
Byte-Oriented Memory Organization Programs Refer to Virtual Addresses – Conceptually very large array of bytes – Actually implemented with hierarchy of different memory types – System provides address space private to particular “process” Program being executed Program can clobber its own data, but not that of others Compiler + Run-Time System Control Allocation – Where different program objects should be stored – All allocation within single virtual address space 000 FFF
Machine Words Machine Has “Word Size” – Nominal size of integer-valued data Including addresses – Most current machines use 32 bits (4 bytes) words Limits addresses to 4GB Becoming too small for memory-intensive applications – High-end systems use 64 bits (8 bytes) words Potential address space ≈ 1.8 X bytes x86-64 machines support 48-bit addresses: 256 Terabytes – Machines support multiple data formats Fractions or multiples of word size Always integral number of bytes
Word-Oriented Memory Organization Addresses Specify Byte Locations – Address of first byte in word – Addresses of successive words differ by 4 (32-bit) or 8 (64-bit) bit Words BytesAddr bit Words Addr = ?? Addr = ?? Addr = ?? Addr = ?? Addr = ?? Addr = ??
Data Representations C Data TypeTypical 32-bitIntel IA32x86-64 char111 short222 int444 long float444 double888 long double810/1210/16 pointer448
Byte Ordering How should bytes within a multi-byte word be ordered in memory? Conventions – Big Endian: Sun, PPC Mac, Internet Least significant byte has highest address – Little Endian: x86 Least significant byte has lowest address
Byte Ordering Example Big Endian – Least significant byte has highest address Little Endian – Least significant byte has lowest address Example – Variable x has 4-byte representation 0x – Address given by &x is 0x100 0x1000x1010x1020x x1000x1010x1020x Big Endian Little Endian
AddressInstruction CodeAssembly Rendition :5b pop %ebx :81 c3 ab add $0x12ab,%ebx c:83 bb cmpl $0x0,0x28(%ebx) Reading Byte-Reversed Listings Disassembly – Text representation of binary machine code – Generated by program that reads the machine code Example Fragment Deciphering Numbers – Value: 0x12ab – Pad to 32 bits: 0x000012ab – Split into bytes: ab – Reverse: ab