The Design of C: A Rational Reconstruction

Similar documents
The Design of C: A Rational Reconstruction" Jennifer Rexford!

The Design of C: A Rational Reconstruction"

The Design of C: A Rational Reconstruction

Goals for this Week. CSC 2400: Computer Systems. Bits, Bytes and Data Types. Binary number system. Finite representations of binary integers

Goals of C "" The Goals of C (cont.) "" Goals of this Lecture"" The Design of C: A Rational Reconstruction"

9/23/15. Agenda. Goals of this Lecture. For Your Amusement. Number Systems and Number Representation. The Binary Number System

Fundamentals of Programming Session 2

Princeton University Computer Science 217: Introduction to Programming Systems. Goals of this Lecture. Number Systems and Number Representation

Number Systems and Number Representation

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

Positional notation Ch Conversions between Decimal and Binary. /continued. Binary to Decimal

Number representations

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

CS 31: Introduction to Computer Systems. 03: Binary Arithmetic January 29

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2

Chapter 2 Bits, Data Types, and Operations

Chapter 2 Bits, Data Types, and Operations

But first, encode deck of cards. Integer Representation. Two possible representations. Two better representations WELLESLEY CS 240 9/8/15

Number Systems CHAPTER Positional Number Systems

Applied Computer Programming

Number System. Introduction. Decimal Numbers

Arithmetic and Bitwise Operations on Binary Data

2.1. Unit 2. Integer Operations (Arithmetic, Overflow, Bitwise Logic, Shifting)

Beginning C Programming for Engineers

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

The Design of C: A Rational Reconstruction (cont.)

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

Signed Binary Numbers

Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

Binary Representations and Arithmetic

Chapter 4. Operations on Data

Bits, Words, and Integers

Recap from Last Time. CSE 2021: Computer Organization. It s All about Numbers! 5/12/2011. Text Pictures Video clips Audio

The Design of C: A Rational Reconstruction (cont.)" Jennifer Rexford!

Princeton University Computer Science 217: Introduction to Programming Systems The C Programming Language Part 1

Number Systems. Both numbers are positive

CS 31: Intro to Systems Binary Arithmetic. Kevin Webb Swarthmore College January 26, 2016

Lecture (01) Digital Systems and Binary Numbers By: Dr. Ahmed ElShafee

Chapter 2 Bits, Data Types, and Operations

2/12/17. Goals of this Lecture. Historical context Princeton University Computer Science 217: Introduction to Programming Systems

Data Representation 1

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

Princeton University Computer Science 217: Introduction to Programming Systems The Design of C

Work relative to other classes

Jin-Soo Kim Systems Software & Architecture Lab. Seoul National University. Integers. Spring 2019

Continued from previous lecture

COMP Overview of Tutorial #2

CS 101: Computer Programming and Utilization

Representing and Manipulating Integers Part I

Programming in C++ 5. Integral data types

Chapter 2 Bits, Data Types, and Operations

Numbers and Computers. Debdeep Mukhopadhyay Assistant Professor Dept of Computer Sc and Engg IIT Madras

Number Systems Standard positional representation of numbers: An unsigned number with whole and fraction portions is represented as:

CHAPTER V NUMBER SYSTEMS AND ARITHMETIC

Inf2C - Computer Systems Lecture 2 Data Representation

CS61B Lecture #14: Integers. Last modified: Wed Sep 27 15:44: CS61B: Lecture #14 1

Arithmetic and Bitwise Operations on Binary Data

Kinds Of Data CHAPTER 3 DATA REPRESENTATION. Numbers Are Different! Positional Number Systems. Text. Numbers. Other

Number Systems. Readings: , Problem: Implement simple pocket calculator Need: Display, adders & subtractors, inputs

Lecture Notes on Ints

THE LOGIC OF COMPOUND STATEMENTS

A flow chart is a graphical or symbolic representation of a process.

A complement number system is used to represent positive and negative integers. A complement number system is based on a fixed length representation

Lecture 3. More About C

9/3/2015. Data Representation II. 2.4 Signed Integer Representation. 2.4 Signed Integer Representation

Chapter 2 Bits, Data Types, and Operations

Semester Transition Point. EE 109 Unit 11 Binary Arithmetic. Binary Arithmetic ARITHMETIC

Chapter 3: Operators, Expressions and Type Conversion

Introduction to Computers and Programming. Numeric Values

CPS 104 Computer Organization and Programming Lecture-2 : Data representations,

Values, Variables, Types & Arithmetic Expressions. Agenda

IT 1204 Section 2.0. Data Representation and Arithmetic. 2009, University of Colombo School of Computing 1

CMPSCI 145 MIDTERM #1 Solution Key. SPRING 2017 March 3, 2017 Professor William T. Verts

DLD VIDYA SAGAR P. potharajuvidyasagar.wordpress.com. Vignana Bharathi Institute of Technology UNIT 1 DLD P VIDYA SAGAR

EE292: Fundamentals of ECE

Electronic Data and Instructions

CSCI 2212: Intermediate Programming / C Chapter 15

Final Labs and Tutors

Introduction to Computer Science-103. Midterm

More about Binary 9/6/2016

CS61B Lecture #14: Integers

Lecture (01) Introduction Number Systems and Conversion (1)

LAB A Translating Data to Binary

Numeral Systems. -Numeral System -Positional systems -Decimal -Binary -Octal. Subjects:

Lecture 13 Bit Operations

Digital Logic. The Binary System is a way of writing numbers using only the digits 0 and 1. This is the method used by the (digital) computer.

Advanced Computer Architecture-CS501

The Design of C: A Rational Reconstruction: Part 2

Data Representation COE 301. Computer Organization Prof. Muhamed Mudawar

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

DRAM uses a single capacitor to store and a transistor to select. SRAM typically uses 6 transistors.

Data Representation. DRAM uses a single capacitor to store and a transistor to select. SRAM typically uses 6 transistors.

Microcomputers. Outline. Number Systems and Digital Logic Review

CS 31: Intro to Systems Binary Arithmetic. Martin Gagné Swarthmore College January 24, 2016

Introduction to Computer Science. Homework 1

Number Systems and Binary Arithmetic. Quantitative Analysis II Professor Bob Orr

Lecture 12 Integers. Computer and Network Security 19th of December Computer Science and Engineering Department

Homework 1 graded and returned in class today. Solutions posted online. Request regrades by next class period. Question 10 treated as extra credit

Transcription:

The Design of C: A Rational Reconstruction 2 Goals of this Lecture Help you learn about: The decisions that were available to the designers of C The decisions that were made by the designers of C and thereby C! Why? Learning the design rationale of the C language provides a richer understanding of C itself and is easier than learning rote rules A systems programmer knows the language to know whatʼs safe and whatʼs not But first a preliminary topic 3

Preliminary Topic Number Systems 4 Why Bits (Binary Digits)? Computers are built using digital circuits Inputs and outputs can have only two values True (high voltage) or false (low voltage) Represented as and 0 Can represent many kinds of information Boolean (true or false) Numbers (23, 79, ) Characters (ʻaʼ, ʻzʼ, ) Pixels, sounds Internet addresses Can manipulate in many ways Read and write Logical operations Arithmetic 5 Base 0 and Base 2 Decimal (base 0) Each digit represents a power of 0 473 = 4 x 0 3 + x 0 2 + 7 x 0 + 3 x 0 0 Binary (base 2) Each bit represents a power of 2 00 = x 2 4 + 0 x 2 3 + x 2 2 + x 2 + 0 x 2 0 = 22 Decimal to binary conversion: Divide repeatedly by 2 and keep remainders 2/2 = 6 R = 0 6/2 = 3 R = 0 3/2 = R = /2 = 0 R = Result = 00 6 2

Writing Bits is Tedious for People Octal (base 8, 3 bits/digit) Digits 0,,, 7 Hexadecimal (base 6, 4 bits/digit) Digits 0,,, 9, A, B, C, D, E, F 0000 = 0 000 = 8 000 = 00 = 9 000 = 2 00 = A 00 = 3 0 = B 000 = 4 00 = C 00 = 5 0 = D 00 = 6 0 = E 0 = 7 = F Thus the 6-bit binary number 0 000 00 00 converted to hex is B2A9 7 The Rise and Fall of Octal Octal (base 8, 3 bits/digit) Digits 0,,, 7 Early computers often had 36 bits/word Competition was high-precision (0-digit) calculators 2 36 = 6879476736, which is greater than 0 0 Decimal required conversion circuitry Reading and display octal numbers required much less processing than decimal Hexadecimal not easy with some displays (Nixie tubes) 36-bit octal possible in 2 octal digits 8 Representing Colors: RGB Three primary colors Red Green Blue Strength 8-bit number for each color (e.g., two hex digits, 256 values) So, 24 bits to specify a color (256 3 colors ~ 6M colors) In HTML, e.g. course Schedule Web page Red: <span style="color:#ff0000">de-comment Assignment Due</ span> Blue: <span style="color:#0000ff">reading Period</span> Same thing in digital cameras Each processed pixel is a mixture of red, green, and blue 9 3

Finite Representation of Integers Fixed number of bits in memory Usually 8, 6, or 32 bits (, 2, or 4 bytes) Unsigned integer No sign bit Always 0 or a positive number All arithmetic is modulo 2 n Examples of unsigned integers 0000000 0000 5 0000000 6 000000 33 255 0 Adding Two Integers From right to left, we add each pair of digits We write the sum, and add the carry to the next column Base 0 Base 2 9 8 + 2 6 4 Sum Carry 4 0 6 2 0 + 0 0 Sum Carry 0 0 0 Binary Sums and Carries a b Sum a b Carry 0 0 0 0 0 0 0 0 0 0 0 0 0 XOR ( exclusive OR ) AND 000 00 + 00 0 00 00 69 03 72 2 4

Modulo Arithmetic Consider only numbers in a range E.g., five-digit car odometer: 0,,, 99999 E.g., eight-bit numbers 0,,, 255 Roll-over when you run out of space E.g., car odometer goes from 99999 to 0,, E.g., eight-bit number goes from 255 to 0,, Adding 2 n doesnʼt change the answer For eight-bit number, n=8 and 2 n =256 E.g., (37 + 256) mod 256 is simply 37 This can help us do subtraction Suppose you want to compute a b Note that this equals a + 256 b, which is also a + (256 - - b) + 3 Oneʼs and Twoʼs Complement Oneʼs complement: flip every bit E.g., b is 00000 (i.e., 69 in decimal) Oneʼs complement is 000 Thatʼs simply 255-69 Subtracting from is easy (no carry needed!) - 000 00 0 00 b oneʼs complement Twoʼs complement Add to the oneʼs complement E.g., (255 69) + 0 0 4 Putting it All Together Computing a b Same as a + 256 b Same as a + (255 b) + Same as a + onescomplement(b) + Same as a + twoscomplement(b) Example: 72 69 The original number 69: 000 00 Oneʼs complement of 69: 0 00 Twoʼs complement of 69: 0 0 Add to the number 72: 00 00 The sum comes to: 00 0 Equals: 03 in decimal 00 00 + 0 0 00 0 5 5

Signed Integers Sign-magnitude representation Use one bit to store the sign Zero for positive number One for negative number Examples E.g., 000 00 44 E.g., 00 00-44 Hard to do arithmetic this way, so it is rarely used Complement representation Oneʼs complement Flip every bit E.g., 0 00-44 Twoʼs complement Flip every bit, then add E.g., 0 000-44 6 Overflow: Running Out of Room Adding two large integers together Sum might be too large to store in the number of bits available What happens? Unsigned integers All arithmetic is modulo arithmetic Sum would just wrap around Signed integers Can get nonsense values Example with 6-bit integers Sum: 0000+20000+30000 Result: -5536 7 Bitwise Operators: AND and OR Bitwise AND (&) & 0 0 0 0 0 Mod on the cheap for certain values! E.g., 53 % 6 is same as 53 & 5; Bitwise OR ( ) 0 0 0 53 & 5 0 0 0 0 0 0 0 0 5 0 0 0 0 0 0 8 6

Bitwise Operators: Not and XOR Oneʼs complement (~) Turns 0 to, and to 0 E.g., set last three bits to 0 x = x & ~7; XOR (^) 0 if both bits are the same if the two bits are different ^ 0 0 0 0 9 Bitwise Operators: Shift Left/Right Shift left (<<): Multiply by powers of 2 Shift some # of bits to the left, filling the blanks with 0 53 53<<2 0 0 0 0 0 0 0 0 0 Shift right (>>): Divide by powers of 2 Shift some # of bits to the right For unsigned integer, fill in blanks with 0 What about signed negative integers? Undefined by language spec two common approaches 53 53>>2 0 0 0 0 0 0 0 0 0 20 Example: Counting the ʼs How many bits in a number? E.g., how many bits in the binary representation of 53? 0 0 0 0 Four bits How to count them? Look at one bit at a time Check if that bit is a Increment counter How to look at one bit at a time? Look at the last bit: n & Check if it is a : (n & ) ==, or simply (n & ) 2 7

Counting the Number of ʻʼ Bits #include <stdio.h> #include <stdlib.h> int main(void) { unsigned int n; unsigned int count; printf("number: "); if (scanf("%u", &n)!= ) { fprintf(stderr, "Error: Expect unsigned int.\n"); exit(exit_failure); } for (count = 0; n > 0; n >>= ) count += (n & ); printf("number of bits: %u\n", count); return 0; } 22 Summary Computer represents everything in binary Integers, floating-point numbers, characters, addresses, Pixels, sounds, colors, etc. Binary arithmetic through logic operations Sum (XOR) and Carry (AND) Twoʼs complement for subtraction Bitwise operators AND, OR, NOT, and XOR Shift left and shift right Useful for efficient and concise code, though sometimes cryptic 23 The Main Event The Design of C 24 8

Goals of C Designers wanted C to support: Systems programming Development of Unix OS Development of Unix programming tools But also: Applications programming Development of financial, scientific, etc. applications Systems programming was the primary intended use 25 The Goals of C (cont.) The designers of wanted C to be: Low-level Close to assembly/machine language Close to hardware But also: Portable Yield systems software that is easy to port to differing hardware 26 The Goals of C (cont.) The designers wanted C to be: Easy for people to handle Easy to understand Expressive High (functionality/sourcecodesize) ratio But also: Easy for computers to handle Easy/fast to compile Yield efficient machine language code Commonality: Small/simple 27 9

Design Decisions In light of those goals What design decisions did the designers of C have? What design decisions did they make? Consider programming language features, from simple to complex 28 Feature : Data Types Previously in this lecture: Bits can be combined into bytes Our interpretation of a collection of bytes gives it meaning A signed integer, an unsigned integer, a RGB color, etc. A data type is a well-defined interpretation of a collection of bytes A high-level programming language should provide primitive data types Facilitates abstraction Facilitates manipulation via associated well-defined operators Enables compiler to check for mixed types, inappropriate use of types, etc. 29 Primitive Data Types Issue: What primitive data types should C provide? Thought process C should handle: Integers Characters Character strings Logical (alias Boolean) data Floating-point numbers C should be small/simple Decisions Provide integer, character, and floating-point data types Do not provide a character string data type (More on that later) Do not provide a logical data type (More on that later) 30 0

Integer Data Types Issue: What integer data types should C provide? Thought process For flexibility, should provide integer data types of various sizes For portability at application level, should specify size of each data type For portability at systems level, should define integral data types in terms of natural word size of computer Primary use will be systems programming Why? Why? 3 Integer Data Types (cont.) Decisions Provide three integer data types: short, int, and long Do not specify sizes; instead: int is natural word size 2 <= bytes in short <= bytes in int <= bytes in long Incidentally, on hats using gcc27 Natural word size: 4 bytes short: 2 bytes int: 4 bytes long: 4 bytes 32 Integer Constants Issue: How should C represent integer constants? Thought process People naturally use decimal Systems programmers often use binary, octal, hexadecimal Decisions Was that a good Use decimal notation as default decision? Use "0" (zero) prefix to indicate octal notation Use "0x" prefix to indicate hexadecimal notation Do not allow binary notation; too verbose, error prone Use "L" suffix to indicate long constant Do not use a suffix to indicate short constant; instead must use cast Examples Why? int: 23, -23, 073, 0x7B long: 23L, -23L, 073L, 0x7BL short: (short)23, (short)-23, (short)073, (short)0x7b 33

Unsigned Integer Data Types Issue: Should C have both signed and unsigned integer data types? Thought process Must represent positive and negative integers Signed types are essential Unsigned data can be twice as large as signed data Unsigned data could be useful Unsigned data are good for bit-level operations Bit-level operations are common in systems programming Implementing both signed and unsigned data types is complex Must define behavior when an expression involves both 34 Unsigned Integer Data Types (cont.) Decisions Provide unsigned integer types: unsigned short, unsigned int, and unsigned long Conversion rules in mixed-type expressions are complex Generally, mixing signed and unsigned converts signed to unsigned See King book Section 7.4 for details Do you see Was providing unsigned types a good decision? What decision did the designers of Java make? any potential problems? 35 Unsigned Integer Constants Issue: How should C represent unsigned integer constants? Thought process L suffix distinguishes long from int; also could use a suffix to distinguish signed from unsigned Octal or hexadecimal probably are used with bit-level operators Decisions Default is signed Use "U" suffix to indicate unsigned Integers expressed in octal or hexadecimal automatically are unsigned Examples unsigned int: 23U, 073, 0x7B unsigned long: 23UL, 073L, 0x7BL unsigned short: (short)23u, (short)073, (short)0x7b 36 2