15213 Recitation Section C Introduction Unix and C Playing with Bits Practice Problems Shimin Chen Sept. 9, 2002 Outline.

Slides:



Advertisements
Similar presentations
A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
Advertisements

Introduction to C Systems Programming Concepts. Introduction to C A simple C Program A simple C Program –Variable Declarations –printf ( ) Compiling and.
Intro to Computer Systems Recitation #1 By sseshadr.
COMP3221: Microprocessors and Embedded Systems--Lecture 1 1 COMP3221: Microprocessors and Embedded Systems Lecture 3: Number Systems (I)
Binary Representation Introduction to Computer Science and Programming I Chris Schmidt.
Types and Variables. Computer Programming 2 C++ in one page!
CS 61C L03 Introduction to C (1)Harvey / Wawrzynek Fall 2003 © UCB 8/29/2003  Brian Harvey ( John Wawrzynek  (Warznek) (
CS 61C L02 Number Representation (1)Harvey / Wawrzynek Fall 2003 © UCB 8/27/2003  Brian Harvey ( John Wawrzynek  (Warznek) (
Data Representation Computer Organization &
Data Representation COE 205
1 Lecture 3 Bit Operations Floating Point – 32 bits or 64 bits 1.
CS 61C L02 Number Representation (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Quiz 1.1 Convert the following unsigned binary numbers to their decimal equivalent: Number2 Number
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
Number System and Codes
Unsigned and Signed Numbers. Hexadecimal Number 217A 16 Position Digits A Value = 2x x x16 + Ax1 = 2x x x16.
How to Write a.c File : Introduction to Computer Systems Recitation 6, Oct 1, 2012 Alexander Malyshev (amalyshe) Section A, 10:30a – 11:20p, WeH.
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
C PROGRAMMING LECTURE 17 th August IIT Kanpur C Course, Programming club, Fall by Deepak Majeti M-Tech CSE
3. Representing Integer Data
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
Binary numbers and arithmetic. ADDITION Addition (decimal)
ECE 2110: Introduction to Digital Systems Signed Number Conversions.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Integer Data representation Addition and Multiplication.
CENG 311 Machine Representation/Numbers
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
Computers Organization & Assembly Language
NUMBER REPRESENTATION CHAPTER 3 – part 3. ONE’S COMPLEMENT REPRESENTATION CHAPTER 3 – part 3.
Binary Arithmetic & Data representation
1 Bit Operator. Advanced Compiler Laboratory2 Bit operators ! : invert logical value if value is 0 change to 1, otherwise set to 0 ~ : invert all bits.
Lec 3: Data Representation Computer Organization & Assembly Language Programming.
1 計算機程式設計 Introduction to Computer Programming Lecture01: Introduction and Hello World 9/10/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction.
The Teacher CP4 Binary and all that… CP4 Revision.
Positional Number Systems
Week 2 - Wednesday.  What did we talk about last time?  C compilation model  Lab 1.
Summary. Data Types int A; I am declaring a variable A, which is an integer.
The Teacher CP4 Binary and all that… CP4 Revision.
Number Systems Binary to Decimal Octal to Decimal Hexadecimal to Decimal Binary to Octal Binary to Hexadecimal Two’s Complement.
Chapter 19 Number Systems. Irvine, Kip R. Assembly Language for Intel-Based Computers, Translating Languages English: Display the sum of A times.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
CSCE 548 Integer Overflows Format String Problem.
CS 101 – Aug. 31 Interpreting binary  decimal √ Decimal  binary Shorthand ways of expressing binary –Octal –“Hex” Negative numbers.
Introduction to C For the participants of Practical Course „Scientific Computing and Visualization“
COMPUTER SCIENCE Data Representation and Machine Concepts Section 1.6 Instructor: Lin Chen Sept 2013.
Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, Signed Integers The highest bit indicates the sign. 1 = negative, 0 = positive.
Cs641 pointers/numbers.1 Overview °Stack and the Heap °malloc() and free() °Pointers °numbers in binary.
Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=
1 Carnegie Mellon Welcome to recitation! : Introduction to Computer Systems 3 rd Recitation, Sept. 10, 2012 Instructor: Adrian Trejo (atrejo) Section.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
1 COMS 161 Introduction to Computing Title: Computing Basics Date: September 8, 2004 Lecture Number: 7.
Number Representation and Arithmetic Circuits
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
Announcements Assignment 1 will be regraded for all who’s score (not percentage) is less than 6 (out of 65). If your score is 6 or higher, but you feel.
Announcements Partial Credit Due Date for Assignment 2 now due on Sat, Feb 27 I always seem to be behind and get tons of daily. If you me and.
1  For recitations  Amr Mahmoud  Office Hours: Monday Benedum Hall.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
1 Integer Representations V1.0 (22/10/2005). 2 Integer Representations  Unsigned integer  Signed integer  Sign and magnitude  Complements  One’s.
Binary Addition The simplest arithmetic operation in binary is addition. Adding two single-digit binary numbers is relatively simple, using a form of carrying:
CSC 482/582: Computer Security
Lec 3: Data Representation
The Machine Model Memory
Chapter 18 I/O in C.
Binary numbers and arithmetic
Computer Systems and Networks
Created by Hwansoo Han Edited by Ikjun Yeom
GCSE COMPUTER SCIENCE Topic 3 - Data 3.3 Logical and Arithmetic Shifts.
Chapter 1 Introduction.
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

15213 Recitation Section C Introduction Unix and C Playing with Bits Practice Problems Shimin Chen Sept. 9, 2002 Outline

15213 Recitation CShimin Chen2 Introducing Myself Try to pronounce my name: Shimin Chen My office hour: Wed 2-3pm, WeH 8019 Contact: – –Phone: x8-5143

15213 Recitation CShimin Chen3 Unix and C Getting to know Makefile PATH environment Common pitfalls of C programming

15213 Recitation CShimin Chen4 Makefile: writing rules Rule Format: targets : prerequisites command... Example: btest: btest.c bits.c decl.c tests.c btest.h bits.h gcc -O -Wall -o btest bits.c btest.c decl.c tests.c tab

15213 Recitation CShimin Chen5 Makefile: using variables CC = gcc CFLAGS = -O -Wall btest: btest.c bits.c decl.c tests.c btest.h bits.h $(CC) $(CFLAGS) -o btest bits.c btest.c decl.c tests.c The value of a variable is the string after the “=”

15213 Recitation CShimin Chen6 L1 Makefile # Student's Makefile for the CS:APP Data Lab TEAM = ac00 VERSION = 1 HANDINDIR = /afs/cs.cmu.edu/academic/class/15213-f02/L1/handin CC = gcc CFLAGS = -O -Wall btest: btest.c bits.c decl.c tests.c btest.h bits.h $(CC) $(CFLAGS) -o btest bits.c btest.c decl.c tests.c handin: cp bits.c $(HANDINDIR)/$(TEAM)-$(VERSION)-bits.c clean: rm -f *.o btest

15213 Recitation CShimin Chen7 Makefile Reference GNU Make Manual –Do a google search for “GNU Make Manual” –

15213 Recitation CShimin Chen8 PATH environment What is PATH? –The directories to search for executable commands How come the Unix shell can't find my program “btest"? –“.” is not in the PATH –./btest –Add “.” into PATH: –

15213 Recitation CShimin Chen9 Pitfalls of C Programming (1) main () { …. for (int i=0; i<10; i++) { …… } …. } Local Variables should be declared at the beginning of the procedure

15213 Recitation CShimin Chen10 Declaring Local Variables main () {int i; …. for (i=0; i<10; i++) { …… } …. }

15213 Recitation CShimin Chen11 Pitfalls of C Programming (2) No cout, cin! Use printf, scanf #include main () { int i = 15213; float f = ; char str[20] = “15213”; printf (“integer: %d\n”, i); printf (“float: %f\n”, f); printf (“string: %s\n”, str); }

15213 Recitation CShimin Chen12 Pitfalls of C Programming (3) #include main () {int i; char str[20]; scanf (“%d”, i); scanf (“%s”, str); } scanf requires an address

15213 Recitation CShimin Chen13 Using Addresses to Call scanf #include main () {int i; char str[20]; scanf (“%d”, &i); scanf (“%s”, str); }

15213 Recitation CShimin Chen14 Pitfalls of C Programming (4) No “new”, “delete” operators! Use “malloc” and “free” #include aProcedure () {char *buffer; buffer = (char *)malloc (4096); if (buffer ==NULL) { printf (“can’t allocate memory!\n”); exit (1); } …….. free (buffer); }

15213 Recitation CShimin Chen15 Turn on the Warnings Compile with “-Wall” Check and fix the warnings

15213 Recitation CShimin Chen16 Playing with Bits Powers of 2 Binary, Hexadecimal, Decimal Unsigned and Two’s Complement

15213 Recitation CShimin Chen17 Powers of 2 Let’s write down 2 0 ~ 2 16

15213 Recitation CShimin Chen18 Hexadecimal Digits 0~9, A, B, C, D, E, F What are the corresponding decimals? What are the corresponding binary numbers?

15213 Recitation CShimin Chen19 Hexadecimal  Binary Let’s convert (3B6D) 16 into Binary: Answer: ( ) 2 Let’s convert ( ) 2 into hexadecimal Answer: (C493) 16

15213 Recitation CShimin Chen20 Binary  Decimal Let’s convert (101010) 2 to decimal Answer: 42 Let’s convert (37) 10 to binary Answer: (100101) 2

15213 Recitation CShimin Chen21 Unsigned and Two’s Complement 4-bit integer What are the decimals for the following unsigned representations? 0000, 1111, 0101, 1000, 0111, 1011 What are the decimals if they are two’s complements?

15213 Recitation CShimin Chen22 Practice Problem (1) Negate the following 8-bit two’s complement: X 1 = X 1 =? X 2 = X 2 =? X 3 = X 3 =? X 4 = X 4 =? Complement then add 1

15213 Recitation CShimin Chen23 Practice Problem (2) Extract a bit from an integer: int extract_a_bit (int x, int pos) { ??? } e.g. extract_a_bit (2, 1) = 1 extract_a_bit (2, 5) = 0

15213 Recitation CShimin Chen24 A Solution int extract_a_bit (int x, int pos) { return ((x>>pos)&1); }

15213 Recitation CShimin Chen25 Practice Problem (3) Compute the bit parity of an integer. Return 1 if there are odd number of 1s; return 0 if there are even number of 1s. int bit_parity (int x) { ??? }

15213 Recitation CShimin Chen26 A Solution int bit_parity (int x) { int word16 = x ^ (x>>16); int word8 = word16 ^ (word16 >> 8); int word4 = word8 ^ (word8 >> 4); int word2 = word4 ^ (word4 >> 2); int bit = word2 ^ (word2 >> 1); return bit & 1; }

15213 Recitation CShimin Chen27 Common Questions of L1 X << 32 doesn’t work! –Some compilers produce: X << (shift&0x1f) Right shift: –Logical for unsigned int –Arithmetic (sign extended) for signed int (not standard, but almost all compilers do this)