binary.c

1/1

lectures/7/src/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38:

/**************************************************************************** * binary.c * * Computer Science 50 * David J. Malan * * Displays a number in binary. * * Demonstrates bitwise operators. ***************************************************************************/ #include #include int main(void) { // prompt user for number int n; do { printf("Non-negative integer please: "); n = GetInt(); } while (n < 0); // print number in binary for (int i = sizeof(int) * 8 - 1; i >= 0; i--) { int mask = 1 << i; if (n & mask) printf("1"); else printf("0"); } printf("\n"); }

bmp.h

1/2

lectures/7/src/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64:

/**************************************************************************** * bmp.h * * Computer Science 50 * Problem Set 5 * * BMP-related data types based on Microsoft’s own. ***************************************************************************/ #include /* * Common Data Types * * The data types in this section are essentially aliases for C/C++ * primitive data types. * * Adapted from http://msdn.microsoft.com/en-us/library/cc230309(PROT.10).aspx. * See http://en.wikipedia.org/wiki/Stdint.h for more on stdint.h. */ typedef typedef typedef typedef

uint8_t uint32_t int32_t uint16_t

BYTE; DWORD; LONG; WORD;

/* * BITMAPFILEHEADER * * The BITMAPFILEHEADER structure contains information about the type, size, * and layout of a file that contains a DIB [device-independent bitmap]. * * Adapted from http://msdn.microsoft.com/en-us/library/dd183374(VS.85).aspx. */ typedef struct { WORD bfType; DWORD bfSize; WORD bfReserved1; WORD bfReserved2; DWORD bfOffBits; } __attribute__((__packed__)) BITMAPFILEHEADER; /* * BITMAPINFOHEADER * * The BITMAPINFOHEADER structure contains information about the * dimensions and color format of a DIB [device-independent bitmap]. * * Adapted from http://msdn.microsoft.com/en-us/library/dd183376(VS.85).aspx. */ typedef struct { DWORD biSize; LONG biWidth; LONG biHeight; WORD biPlanes; WORD biBitCount;

bmp.h lectures/7/src/ 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90:

DWORD biCompression; DWORD biSizeImage; LONG biXPelsPerMeter; LONG biYPelsPerMeter; DWORD biClrUsed; DWORD biClrImportant; } __attribute__((__packed__)) BITMAPINFOHEADER; /* * RGBTRIPLE * * This structure describes a color consisting of relative intensities of * red, green, and blue. * * Adapted from http://msdn.microsoft.com/en-us/library/aa922590.aspx. */ typedef struct { BYTE rgbtBlue; BYTE rgbtGreen; BYTE rgbtRed; } __attribute__((__packed__)) RGBTRIPLE;

2/2

endian.c

1/2

lectures/7/src/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64:

/**************************************************************************** * endian.c * * Computer Science 50 * David J. Malan * * Reads bf.bfSize from a BMP. * * Demonstrates endianness. ***************************************************************************/ #include #include #include int main(int argc, char *argv[]) { // ensure proper usage if (argc != 2) return 1; // open file FILE *fp = fopen(argv[1], "r"); if (fp == NULL) return 1; // seek to BITMAPFILEHEADER’s bfSize fseek(fp, 2, SEEK_SET); // read in BITMAPFILEHEADER’s bfSize uint32_t bfSize; fread(&bfSize, sizeof(bfSize), 1, fp); // print bfSize printf("\nbfSize: %d\n\n", bfSize); // return to start of file rewind(fp); // read in BITMAPFILEHEADER’s raw bytes uint8_t *buffer = malloc(14); fread(buffer, 1, 14, fp); // print field via cast printf("bfSize: %d\n\n", *((uint32_t *) (buffer + 2))); // print individual bytes in decimal printf("bfSize: %d %d %d %d\n", buffer[2], buffer[3], buffer[4], buffer[5]); // print individual bytes in hexadecimal printf("bfSize: 0x%x 0x%x 0x%x 0x%x\n", buffer[2], buffer[3], buffer[4], buffer[5]); // print individual bytes in binary printf("bfSize: "); for (int i = 2; i < 6; i++) { for (int j = 7; j >= 0; j--) { int mask = 1 << j; if (buffer[i] & mask)

endian.c

2/2

lectures/7/src/ 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: }

memory.c

1/1

lectures/7/src/ printf("1"); else printf("0");

} } printf("\n\n"); // that’s all folks return 0;

1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32:

/**************************************************************************** * memory.c * * Computer Science 50 * David J. Malan * * Demonstrates memory-related errors. * * problem 1: heap block overrun * problem 2: memory leak -- x not freed * * Adapted from * http://valgrind.org/docs/manual/quick-start.html#quick-start.prepare. ***************************************************************************/ #include void f(void) { int *x = malloc(10 * sizeof(int)); x[10] = 0; } int main(void) { f(); return 0; }

pointers1.c

1/1

lectures/7/src/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34:

/**************************************************************************** * pointers1.c * * Computer Science 50 * David J. Malan * * Prints a string, one character per line. * * Demonstrates strings as arrays. ***************************************************************************/ #include #include #include int main(void) { // prompt user for string printf("String please: "); char *s = GetString(); if (s == NULL) return 1; // print string, one character per line for (int i = 0, n = strlen(s); i < n; i++) printf("%c\n", s[i]); // free string free(s); return 0; }

pointers2.c

1/1

lectures/7/src/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34:

/**************************************************************************** * pointers2.c * * Computer Science 50 * David J. Malan * * Prints a string, one character per line. * * Demonstrates pointer arithmetic. ***************************************************************************/ #include #include #include int main(void) { // prompt user for string printf("String please: "); char *s = GetString(); if (s == NULL) return 1; // print string, one character per line for (int i = 0, n = strlen(s); i < n; i++) printf("%c\n", *(s+i)); // free string free(s); return 0; }

swap2.c

1/1

lectures/7/src/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45:

1/1

lectures/7/src/

/**************************************************************************** * swap2.c * * Computer Science 50 * David J. Malan * * Swaps two variables’ values. * * Demonstrates (clever use of) bitwise operators. ***************************************************************************/ #include // function prototype void swap(int *a, int *b); int main(void) { int x = 1; int y = 2; printf("x is %d\n", x); printf("y is %d\n", y); printf("Swapping...\n"); swap(&x, &y); printf("Swapped!\n"); printf("x is %d\n", x); printf("y is %d\n", y); } /* * Swap arguments’ values. */ void swap(int { *a = *b = *a = }

tolower.c

*a, int *b) *a ^ *b; *a ^ *b; *a ^ *b;

1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34:

/**************************************************************************** * tolower.c * * Computer Science 50 * David J. Malan * * Converts an uppercase character to lowercase. * * Demonstrates bitwise operators. ***************************************************************************/ #include #include #include int main(void) { // prompt user for an uppercase character char c; do { printf("Uppercase character please: "); c = GetChar(); } while (c < ’A’ || c > ’Z’); // print number in lowercase printf("%c\n", c | 0x20); // that’s all folks return 0; }

toupper.c

1/1

lectures/7/src/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34:

/**************************************************************************** * toupper.c * * Computer Science 50 * David J. Malan * * Converts a lowercase character to uppercase. * * Demonstrates bitwise operators. ***************************************************************************/ #include #include #include int main(void) { // prompt user for a lowercase character char c; do { printf("Lowercase character please: "); c = GetChar(); } while (c < ’a’ || c > ’z’); // print number in lowercase printf("%c\n", c & 0xdf); // that’s all folks return 0; }

uint.c

1/1

lectures/7/src/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26:

/**************************************************************************** * uint.c * * Computer Science 50 * David J. Malan * * Prints a signed 8-bit integer and an unsigned 8-bit integer. * * Demonstrates signed, fixed-width types. ***************************************************************************/ #include #include int main(void) { // declare and print signed 8-bit integer int8_t i = 0xff; printf("%d\n", i); // declare and print signed 8-bit integer uint8_t u = 0xff; printf("%d\n", u); }

binary.c 1/1 bmp.h 1/2 - CS50 CDN

4: * Computer Science 50. 5: * David J. Malan. 6: * ... 32: * The BITMAPFILEHEADER structure contains information about the type, size,. 33: * and layout of a file ...

42KB Sizes 1 Downloads 208 Views

Recommend Documents

52/cs50! - CS50 CDN
SSH. • Secure Shell. • Allows you to access another computer through command-‐line interface. • We use SSH to connect to the CS50 Cloud!

52/cs50! - CS50 CDN
A condi on may have two values: true or false. • May be expressed as a logical expression or a. 'bool' variable. • Can be thought of as a yes/no ques on, or a.

CS50 Walkthrough 1 - CS50 CDN
Free Candy. ▫ Time for Change. ▫ I Saw You ... Free Candy. ▫ Seriously, in the CS50 ... ask user for an integer printf("Give me an integer between 1 and 10: ");.

CS50 Walkthrough #3 - CS50 CDN
Go to middle if k < value at middle search for k between first and the one before the middle if k > value at middle search for k between one after the middle and last if k = value at middle return true. If you haven't found k after this loop, return

Merge Sort - CS50 CDN
Data stored in memory has both a value and a location. • Pointers contain the memory address of some piece of data. • * pointer contains address to a ...

pset4 - CS50 CDN
Oct 8, 2010 - Go ahead and execute the command below: hostname. Recall that cloud.cs50.net is actually a cluster of servers. That command tells you the name of the specific server in the cluster that you happen to be connected to at the moment. Take

Merge Sort - CS50 CDN
Data stored in memory has both a value and a location. • Pointers contain the memory address of some piece of data. • * pointer contains address to a ...

CS51 - CS50 CDN
We can still conceptualize & prototype using the right language abstractions. ▻ If we understand relationships between linguistic abstractions, we can realize ...

Untitled - CS50 CDN
http://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/Characteristics/Characteristics.html ... content="yes"> http://developer.apple.com/library/safari/documentation/appleapplications/reference/SafariHTMLRef/Article

Asymptotic Notation - CS50 CDN
Like searching through the phone book. • Identify ... as you go. If array[i + 1] < array[i], swap them! ... Grab the smallest and swap it with whatever is at the front of ...

Krzysztof Gajos - CS50 CDN
What you will learn in. CS 179. • Discover and understand people's latent needs. • Invent and construct prototypes. • Design for people different than yourself.

CS50 Walkthrough #3 - CS50 CDN
what type are these values? ▫ how do we initialize them? ▫ don't forget! ▫ swap tiles for even d ... Questions? Please email me feedback: [email protected].

cs50.c 1/5 cs50.c 2/5 - CS50 CDN
11: * Based on Eric Roberts' genlib.h and simpio.h. 12: *. 13: * The latest version of this file can be found at. 14: * http://www.cs50.net/pub/releases/cs50/cs50.h.

Quiz 0 - CS50 CDN
In the context of files, Linux uses \n to end lines, Mac OS uses \r, and Windows ... format string's expectation of a leading %f, and so neither f nor c get filled with a ...

Week 8 - CS50 CDN
PHP: PHP Hypertext Preprocessor. • When accessed, dynamically generates a webpage which it then outputs to browser. • PHP code enclosed in tag.

Untitled - CS50 CDN
void swap(int a, int b). { int tmp = a; a = b; b = tmp;. } Page 11. void swap(int *a, int *b). { int tmp = *a;. *a = *b;. *b = tmp;. } Page 12. main's parameters main's ...

Asymptotic Notation - CS50 CDN
break – tell the program to 'pause' at a certain point (either a function or a line number) step – 'step' to the next executed statement next – moves to the next ...

Krzysztof Gajos - CS50 CDN
What you will learn in. CS 179. • Discover and understand people's latent needs. • Invent and construct prototypes. • Design for people different than yourself.

Untitled - CS50 CDN
void swap(int a, int b). { int tmp = a; a = b; b = tmp;. } Page 11. void swap(int *a, int *b). { int tmp = *a;. *a = *b;. *b = tmp;. } Page 12. main's parameters.

Computer Science 124 - CS50 CDN
Computer Science 124 : Who Should Take It. • CS 124 is all about developing techniques for solving problems. • This is what CS is all about! – Take a problem.

Untitled - CS50 CDN
Mac OS 10.6. Windows 7. Mac OS 10.5. Windows Vista. Windows XP. Mac OS 10.4. Linux. 0. 50. 100. 150. 200. Page 19. Elective. Concentration. Unsure.

Untitled - CS50 CDN
50. 100. 150. 200. Page 19. Elective. Concentration. Unsure. Gen Ed. Core. 0. 50. 100. 150. 200. 250. 300. Page 20. arrays. Page 21. to be continued... Page 22.

Untitled - CS50 CDN
http://www.blogcdn.com/www.engadget.com/media/2008/05/iphone_line_1-1.jpg. Page 16. valgrind valgrind -‐v -‐-‐leak-‐check=full a.out. Invalid write of size 4.

CS121 Tease for CS50.pptx - CS50 CDN
Formal Systems and Computation. • Two ways to look at it. 1. Study of problems and computers with all their physicality abstracted away q0 q1 q2 q3 a a a a b b.