220 likes | 330 Views
ecs30 Winter 2012: Programming and Problem Solving # 02: Chapters 1~2. Dr. S. Felix Wu Computer Science Department University of California, Davis http://www.cs.ucdavis.edu/~wu/ wu@cs.ucdavis.edu. #include < stdio.h > int main () { int x ; /* declare x */ x = 1;
E N D
ecs30 Winter 2012:Programming and Problem Solving#02: Chapters 1~2 Dr. S. Felix Wu Computer Science Department University of California, Davis http://www.cs.ucdavis.edu/~wu/ wu@cs.ucdavis.edu ecs30 winter 2012 Lecture #02
#include <stdio.h> int main () { intx; /* declare x */ x = 1; printf("Hello world %d\n", x); return 0; }// main() ecs30 winter 2012 Lecture #02
printf("Hello world %d\n", x); scanf(“%lf”, &miles); Try this yourself – intx = 999; printf("Hello [%d %d]\n", x, &x); ecs30 winter 2012 Lecture #02
Do I need to know exactly where to store/save the value of the variable “miles”? ecs30 winter 2012 Lecture #02
Memory Cell Concept Again ecs30 winter 2012 Lecture #02
“Address” • Linguistics in C programming Language Where is THIS really located on my device? Then, we need a way to represent “address”! ecs30 winter 2012 Lecture #02
“Address” • 32 bits or 64 bits address 01101001 10110001 11100000 10100111 0x69b1e0a7 ecs30 winter 2012 Lecture #02
Information Representation: We need Memory to store both Programs/Data. But How? bit: 0/1 physical memory. (BInary digiT) byte: 8 bits word: 32 bits Numbers (Decimal/Binary/Hexadecimal): How do we use bits to represent an integer. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ===> Decimal 0, 1 ===> Binary 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f ===> Hexadecimal ecs30 winter 2012 Lecture #02
Numbers: Base: 10, 2, 16 Binary {0,1} 0,1,2,3,4,5,6,7,8,9, 10,11, 0,1,2…,10,11,12,…99,100,101,102,… 0,1,10 (2), 11(3), 100 (4), 101(5), 110(6), 4 2 1 22, 21, 20 1 0 0 1 0 + 1 1 0 ecs30 winter 2012 Lecture #02
0+1 = 1 1 20 1+1 = 10 2 21 10+1 = 11 3 11+1 = 100 4 22 100+1 = 101 5 101+1 = 110 6 110+1 = 111 7 111+1 = 1000 8 23 9 = 1+8 0001 1000 1001 001 010 100 111 001 100 101 7=1+2+4 5 = 1 + 4 ecs30 winter 2012 Lecture #02
0 = 0000 = 0 * 8 + 0 * 4 + 0 * 2 + 0 * 1 1 = 0001 = 0 * 8 + 0 * 4 + 0 * 2 + 1 * 1 2 = 0010 = 0 * 8 + 0 * 4 + 1 * 2 + 0 * 1 3 = 0011 = 0 * 8 + 0 * 4 + 1 * 2 + 1 * 1 ....... 7 = 0111 = 0 * 8 + 1 * 4 + 1 * 2 + 1 * 1 8 = 1000 = 1 * 8 + 0 * 4 + 0 * 2 + 0 * 1 9 = 1001 = 1 * 8 + 0 * 4 + 0 * 2 + 1 * 1 10 (a) = 1010 = 1 * 8 + 0 * 4 + 1 * 2 + 0 * 1 11 (b) = 1011 = 1 * 8 + 0 * 4 + 1 * 2 + 1 * 1 12 (c) = 1100 = 1 * 8 + 1 * 4 + 0 * 2 + 0 * 1 13 (d) = 1101 = 1 * 8 + 1 * 4 + 0 * 2 + 1 * 1 14 (e) = 1110 = 1 * 8 + 1 * 4 + 1 * 2 + 0 * 1 15 (f) = 1111 = 1 * 8 + 1 * 4 + 1 * 2 + 1 * 1 Examples: 09FE (in Hex) == ? (in Binary) == ? (in Decimal) ecs30 winter 2012 Lecture #02
“Memory Box” Each “address-able” box is ONE Byte 1 byte = 8 bits 1 bit is 0 or 1 Binary representation ecs30 winter 2012 Lecture #02
“The story of X” int X; X, &X, *X, ((struct S *) X)->index, ((int *) X)[20], *(((int *) X)+20), ((double *) X)[20], ((struct S *) 0)->index,… ecs30 winter 2012 Lecture #02
Byte versus Double • Each Memory cell is 1 Byte • How about Double? • How will Double map to your hardware memory? ecs30 winter 2012 Lecture #02
Arithmetic Operations in Binary/Bits? +/- 0001 + 0001 = 0010 bit: 0/1 physical memory. byte: 8 bits in Unix, “usually” an integer has 4 bytes, i.e., 32 bits. unsigned 0 ~ 4294967295 (i.e. 2 32 -1) signed -2147483648 ~ 2147483647 (i.e. -(231) ~ (231 - 1)) ecs30 winter 2012 Lecture #02
How do I represent a character? (Internal coding) ASCII (American Standard Code for Information Interchange) use 7 bit to represent a character (might or might not on your keyboard) Example: 'A' 1100001 (in Byte, 0110001) (Appendix A: page 839) Decimal: 65, Hex: 41. Question: How many different characters can we represent? ecs30 winter 2012 Lecture #02
Binary Representation • Integer (int) 4 bytes • Double 8 bytes • Character 1 byte ecs30 winter 2012 Lecture #02
/* an example program for the sizeof operator in C */ #include <stdio.h> main() { printf("The size of an integer is %d.\n", sizeof(int)); printf("The size of a float is %d.\n", sizeof(float)); printf("The size of a double is %d.\n", sizeof(double)); printf("The size of a character is %d.\n", sizeof(char)); } The size of an integer is 4. The size of a float is 4. The size of a double is 8. The size of a character is 1. ecs30 winter 2012 Lecture #02
printf("value = %d, address = %d\n", cats, &cats); ===> value = 3, address = -268438156; **** printf("value = %d, address = %x\n", cats, &cats); ===> value = 3, address = effff574; -268438156 (in decimal) == effff574 (in hex) aa = -268438156; aa = 0xeffff574; ecs30 winter 2012 Lecture #02
{ … } • { … } • {… { … {…}…} … {…}…} • int main (void) { … } • float myfunc (int x) { …; return f;} • { <declarations> <statements> } ecs30 winter 2012 Lecture #02