Download - 15213 Recitation Section C
![Page 1: 15213 Recitation Section C](https://reader035.vdocuments.us/reader035/viewer/2022062309/56814abc550346895db7d0f1/html5/thumbnails/1.jpg)
15213 Recitation Section C
• Introduction• Unix and C• Playing with Bits• Practice Problems
Shimin ChenSept. 9, 2002
Outline
![Page 2: 15213 Recitation Section C](https://reader035.vdocuments.us/reader035/viewer/2022062309/56814abc550346895db7d0f1/html5/thumbnails/2.jpg)
215213 Recitation C Shimin Chen
Introducing Myself
• Try to pronounce my name:
Shimin Chen
• My office hour:
Wed 2-3pm, WeH 8019
• Contact:– Email: [email protected]– Phone: x8-5143
![Page 3: 15213 Recitation Section C](https://reader035.vdocuments.us/reader035/viewer/2022062309/56814abc550346895db7d0f1/html5/thumbnails/3.jpg)
315213 Recitation C Shimin Chen
Unix and C
• Getting to know Makefile
• PATH environment
• Common pitfalls of C programming
![Page 4: 15213 Recitation Section C](https://reader035.vdocuments.us/reader035/viewer/2022062309/56814abc550346895db7d0f1/html5/thumbnails/4.jpg)
415213 Recitation C Shimin Chen
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
![Page 5: 15213 Recitation Section C](https://reader035.vdocuments.us/reader035/viewer/2022062309/56814abc550346895db7d0f1/html5/thumbnails/5.jpg)
515213 Recitation C Shimin Chen
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 “=”
![Page 6: 15213 Recitation Section C](https://reader035.vdocuments.us/reader035/viewer/2022062309/56814abc550346895db7d0f1/html5/thumbnails/6.jpg)
615213 Recitation C Shimin Chen
L1 Makefile# Student's Makefile for the CS:APP Data LabTEAM = ac00VERSION = 1HANDINDIR = /afs/cs.cmu.edu/academic/class/15213-f02/L1/handin
CC = gccCFLAGS = -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
![Page 7: 15213 Recitation Section C](https://reader035.vdocuments.us/reader035/viewer/2022062309/56814abc550346895db7d0f1/html5/thumbnails/7.jpg)
715213 Recitation C Shimin Chen
Makefile Reference
• GNU Make Manual– Do a google search for “GNU Make Manual”– http://www.gnu.org/manual/make/html_node/make_toc.html
![Page 8: 15213 Recitation Section C](https://reader035.vdocuments.us/reader035/viewer/2022062309/56814abc550346895db7d0f1/html5/thumbnails/8.jpg)
815213 Recitation C Shimin Chen
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:– http://www-2.cs.cmu.edu/afs/cs/academic/class/15213-f02/www/
![Page 9: 15213 Recitation Section C](https://reader035.vdocuments.us/reader035/viewer/2022062309/56814abc550346895db7d0f1/html5/thumbnails/9.jpg)
915213 Recitation C Shimin Chen
Pitfalls of C Programming (1)
main ()
{
….
for (int i=0; i<10; i++) {
……
}
….
}
Local Variables should be declared at the beginning of the procedure
![Page 10: 15213 Recitation Section C](https://reader035.vdocuments.us/reader035/viewer/2022062309/56814abc550346895db7d0f1/html5/thumbnails/10.jpg)
1015213 Recitation C Shimin Chen
Declaring Local Variables
main ()
{int i;
….
for (i=0; i<10; i++) {
……
}
….
}
![Page 11: 15213 Recitation Section C](https://reader035.vdocuments.us/reader035/viewer/2022062309/56814abc550346895db7d0f1/html5/thumbnails/11.jpg)
1115213 Recitation C Shimin Chen
Pitfalls of C Programming (2)
• No cout, cin!• Use printf, scanf
#include <stdio.h>main (){ int i = 15213; float f = 1.5213; char str[20] = “15213”;
printf (“integer: %d\n”, i);printf (“float: %f\n”, f);printf (“string: %s\n”, str);
}
![Page 12: 15213 Recitation Section C](https://reader035.vdocuments.us/reader035/viewer/2022062309/56814abc550346895db7d0f1/html5/thumbnails/12.jpg)
1215213 Recitation C Shimin Chen
Pitfalls of C Programming (3)
#include <stdio.h>
main ()
{int i;
char str[20];
scanf (“%d”, i);
scanf (“%s”, str);
}
scanf requires an address
![Page 13: 15213 Recitation Section C](https://reader035.vdocuments.us/reader035/viewer/2022062309/56814abc550346895db7d0f1/html5/thumbnails/13.jpg)
1315213 Recitation C Shimin Chen
Using Addresses to Call scanf
#include <stdio.h>
main ()
{int i;
char str[20];
scanf (“%d”, &i);
scanf (“%s”, str);
}
![Page 14: 15213 Recitation Section C](https://reader035.vdocuments.us/reader035/viewer/2022062309/56814abc550346895db7d0f1/html5/thumbnails/14.jpg)
1415213 Recitation C Shimin Chen
Pitfalls of C Programming (4)
• No “new”, “delete” operators!• Use “malloc” and “free”
#include <malloc.h>aProcedure (){char *buffer;
buffer = (char *)malloc (4096);if (buffer ==NULL) {
printf (“can’t allocate memory!\n”);exit (1);
}……..free (buffer);
}
![Page 15: 15213 Recitation Section C](https://reader035.vdocuments.us/reader035/viewer/2022062309/56814abc550346895db7d0f1/html5/thumbnails/15.jpg)
1515213 Recitation C Shimin Chen
Turn on the Warnings
• Compile with “-Wall”
• Check and fix the warnings
![Page 16: 15213 Recitation Section C](https://reader035.vdocuments.us/reader035/viewer/2022062309/56814abc550346895db7d0f1/html5/thumbnails/16.jpg)
1615213 Recitation C Shimin Chen
Playing with Bits
• Powers of 2
• Binary, Hexadecimal, Decimal
• Unsigned and Two’s Complement
![Page 17: 15213 Recitation Section C](https://reader035.vdocuments.us/reader035/viewer/2022062309/56814abc550346895db7d0f1/html5/thumbnails/17.jpg)
1715213 Recitation C Shimin Chen
Powers of 2
• Let’s write down 20 ~ 216
![Page 18: 15213 Recitation Section C](https://reader035.vdocuments.us/reader035/viewer/2022062309/56814abc550346895db7d0f1/html5/thumbnails/18.jpg)
1815213 Recitation C Shimin Chen
Hexadecimal Digits
• 0~9, A, B, C, D, E, F
• What are the corresponding decimals?
• What are the corresponding binary numbers?
![Page 19: 15213 Recitation Section C](https://reader035.vdocuments.us/reader035/viewer/2022062309/56814abc550346895db7d0f1/html5/thumbnails/19.jpg)
1915213 Recitation C Shimin Chen
Hexadecimal Binary
• Let’s convert (3B6D)16 into Binary:
• Answer: (11101101101101)2
• Let’s convert (1100010010010011)2 into hexadecimal
• Answer: (C493)16
![Page 20: 15213 Recitation Section C](https://reader035.vdocuments.us/reader035/viewer/2022062309/56814abc550346895db7d0f1/html5/thumbnails/20.jpg)
2015213 Recitation C Shimin Chen
Binary Decimal
• Let’s convert (101010)2 to decimal
• Answer: 42
• Let’s convert (37)10 to binary
• Answer: (100101)2
![Page 21: 15213 Recitation Section C](https://reader035.vdocuments.us/reader035/viewer/2022062309/56814abc550346895db7d0f1/html5/thumbnails/21.jpg)
2115213 Recitation C Shimin Chen
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?
![Page 22: 15213 Recitation Section C](https://reader035.vdocuments.us/reader035/viewer/2022062309/56814abc550346895db7d0f1/html5/thumbnails/22.jpg)
2215213 Recitation C Shimin Chen
Practice Problem (1)
• Negate the following 8-bit two’s complement:
X1=01000100 -X1=?
X2=10011000 -X2=?
X3=00000000 -X3=?
X4=10000000 -X4=?
• Complement then add 1
![Page 23: 15213 Recitation Section C](https://reader035.vdocuments.us/reader035/viewer/2022062309/56814abc550346895db7d0f1/html5/thumbnails/23.jpg)
2315213 Recitation C Shimin Chen
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
![Page 24: 15213 Recitation Section C](https://reader035.vdocuments.us/reader035/viewer/2022062309/56814abc550346895db7d0f1/html5/thumbnails/24.jpg)
2415213 Recitation C Shimin Chen
A Solution
int extract_a_bit (int x, int pos)
{
return ((x>>pos)&1);
}
![Page 25: 15213 Recitation Section C](https://reader035.vdocuments.us/reader035/viewer/2022062309/56814abc550346895db7d0f1/html5/thumbnails/25.jpg)
2515213 Recitation C Shimin Chen
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)
{
???
}
![Page 26: 15213 Recitation Section C](https://reader035.vdocuments.us/reader035/viewer/2022062309/56814abc550346895db7d0f1/html5/thumbnails/26.jpg)
2615213 Recitation C Shimin Chen
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;
}
![Page 27: 15213 Recitation Section C](https://reader035.vdocuments.us/reader035/viewer/2022062309/56814abc550346895db7d0f1/html5/thumbnails/27.jpg)
2715213 Recitation C Shimin Chen
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)