1 / 22

System Programming

System Programming. Chapter 2. ASCII. /* This program shows the dual interpretations of char and ** unsigned char data types. (pg 54) */ #include &lt;stdio.h&gt; main() { char a; unsigned char b; a='A'; b='B'; printf(&quot;%c %c %d %d<br>&quot;,a,b,a,b); a=183; b=255; printf(&quot;%d %d<br>&quot;,a,b); }.

vinaya
Download Presentation

System Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. System Programming Chapter 2

  2. ASCII /* This program shows the dual interpretations of char and ** unsigned char data types. (pg 54) */ #include <stdio.h> main() { char a; unsigned char b; a='A'; b='B'; printf("%c %c %d %d\n",a,b,a,b); a=183; b=255; printf("%d %d\n",a,b); }

  3. sizeof() operator /* This program demonstrates the sizeof() operator. (pg 57) */ #include <stdio.h> main() { int i; char c; double d; printf("%d %d %d %d\n",sizeof(i),sizeof(c),sizeof(d),sizeof(float)); }

  4. Bitwise NOT /* This program demonstrates the bitwise not operator. (pg 59) */ #include <stdio.h> main() { unsigned char a; a=17; a=~a; printf("%d\n",a); }

  5. Bitwise AND /* This program demonstrates the bitwise and operator. (pg 60) */ #include <stdio.h> main() { unsigned char a,b; a=17; b=22; a=a & b; printf("%d\n",a); }

  6. Bitwise OR /* This program demonstrates the bitwise or operator. (pg 60) */ #include <stdio.h> main() { unsigned char a,b; a=17; b=22; a=a | b; printf("%d\n",a); }

  7. Bit operators (variables and constants) /* This program demonstrates using the bitwise operators ** with variables and constants. (pg 61) */ #include <stdio.h> main() { char x,y; x=7; y=6; x=x&y; y=x|16; printf("%d %d\n",x,y); }

  8. Left/Right shift /* This program demonstrates the bitwise shift operators, ** left-shift and right-shift. (pg 61) */ #include <stdio.h> main() { unsigned char a,b; a=17; a=a << 2; b=64; b=b >> 3; printf("%d %d\n",a,b); }

  9. Bitwise right-shift (negative) /* This program demonstrates right-shifting a negative integer ** so that the shifted-in bits are 1 instead of 0 (pg 62) */ #include <stdio.h> main() { char a,b; a=17; a=a >> 2; b=-65; b=b >> 2; printf("%d %d\n",a,b); }

  10. Bitmasks /* This program demonstrates setting a bit, clearing a bit, and ** reading a bit. (pg 64) */ #include <stdio.h> main() { char a; int i; a=17; a=a | (1 << 3); /* set 3rd bit */ printf("%d\n",a); a=a & (~(1<<4)); /* clear 4th bit */ printf("%d\n",a); for (i=7; i>=0; i--) printf("%d ",(a&(1<<i)) >> i); /* read i'th bit */ printf("\n"); }

  11. Memory map 1 /* Draw the memory map for this code (pg 65) */ #include <stdio.h> main() { char a,b,c; a=7; b=-13; c=0; }

  12. Memory map 2 /* Draw the memory map for this code (pg 65) */ #include <stdio.h> main() { char a; int b; float c; double d; a=7; b=-13; c=0.1; d=42.5; }

  13. Memory map 3 /* Draw the memory map for this code. Showing the bit patterns ** emphasizes the differences in storage of "6" (pg 66) */ #include <stdio.h> main() { char a; short int b; char c; a=6; b=13; c='6'; }

  14. Memory map 4 /* Draw the memory map for this code. It can emphasize the ** value of a loop counter after the loop is done (pg 67) */ #include <stdio.h> main() { int i,n; n=0; for (i=1; i<=4; i++) n=n+i; }

  15. Memory map 5 /* Draw the memory map for this code. It can emphasize an ** unitialized variable (pgs 67-68) */ #include <stdio.h> main() { int i,sum; printf("%d\n",sum); for (i=1; i<=10; i++) if (i%2 == 0) sum=sum+i; printf("%d\n",sum); }

  16. Problem 1 /* Code for problem 1 - draw the memory map (pg 68) */ #include <stdio.h> main() { char c=35; char d='G'; int x=-42; float f=17.25; int i=1099563008; double a=17.25; }

  17. Problem 9 /* Code for problem #9 in chapter 2 (pg 70) */ #include <stdio.h> main() { unsigned char x,y,z; x=15; y=35; z=133; x=x|64; y=y&3; z=~z; printf("%d %d %d\n",x,y,z); }

  18. Problem 10 /* Code for problem #10 in chapter 2 (pg 70) */ #include <stdio.h> main() { int x=7; x=(x|16)<<1; printf("%d\n",x); }

  19. Problem 11 /* Code for problem #11 in chapter 2 (pg 70) */ #include <stdio.h> main() { char i; double d; int t; t=0; for (i='z'; i>='w'; i--) for (d=1.0; d<=1.5; d+=0.1) if (d-1.3 > 0) t++; printf("%d\n",t); }

  20. Problem 12 /* Code for problem #12 in chapter 2 (pg 70) */ #include <stdio.h> main() { int i,j; j=0; for (i=1; i<100; i=i<<1) { if (i % 5 > 1) j=j | i; printf("%d %d\n",i,j); } }

  21. Problem 13 /* Code for problem #13 in chapter 2 (pg 71) */ #include <stdio.h> main() { int i,j,k; j=0; k=32; for (i=100; i>0; i-=10) { if (k/4 > 0) j=j | k; if (i <= 70 && i >= 40) k=k<<1; else k=k>>1; printf("%d %d %d\n",i,j,k); } }

  22. Problem 17 /* This is the code for problem #17 in chapter 2 (pg 72) */ #include <stdio.h> main() { int i; double d; char s[10]; s[0]='f'; s[1]='r'; s[2]='o'; s[3]='g'; d=0.0; for (i=0; i<4; i++) d=d+(double)(s[i]-'a'); }

More Related