Advertisement
wavec022

lesson 2 c intro and lesson 3 storage

Aug 19th, 2020 (edited)
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. C language directives
  2.  
  3. #if, #ifdef, ifndef, endif, else, elif, include, define, undef, pragma, error, line
  4.  
  5. Compile with the -Wall flag!
  6.  
  7. WORKSHEET
  8.  
  9. int isPrime(int n) {
  10. int i;
  11. if(n < 2) return 0;
  12. for(i=2; i<n/2+1; i++) {
  13. if(n%i == 0) {
  14. return 0;
  15. }
  16. }
  17. return 1;
  18. }
  19.  
  20. int sumPrimes(int x) {
  21. int total = 0;
  22. int i;
  23. for(i=2; i<=x; i++) {
  24. if(isPrime(i)) {
  25. total += i;
  26. }
  27. }
  28. return total;
  29. }
  30.  
  31. =====
  32.  
  33. Lesson 3
  34.  
  35. C is a weakly typed language-- associates a type with every value, allows for implicit conversions between some types
  36.  
  37. Types only exist at the code level; machine level has no knowledge of types
  38.  
  39. A byte is the smallest addressable unit of memory-- 8 bits
  40.  
  41. Binary representation of data changes based on data type:
  42. - unsigned (traditional)
  43. - two's complement (signed)
  44. - fixed/floating point
  45.  
  46. Hexadecimal used to represent bytes-- 0x00 to 0xFF
  47. - string ex3: 65 78 33
  48.  
  49. Little endian systems (x86)-- MSB is later address: 33 78 65
  50. Big endian (sun. ibm)-- MSB at earlier address: 65 78 33
  51.  
  52. =====
  53.  
  54. Bitwise Operators-- | & ^
  55. || != && are logical operators (byte level)
  56.  
  57. >> and << are bitwise shift
  58. - Left shift: 67 << 5 = 01000011 << 5 = 01100000 = 96
  59. - Logical right shift (unsigned)
  60. - Arithmetic right shift (signed) -- pad with zeros if first is 0
  61. - Arithmetic right shift (signed) -- pad with 1 if first is 1
  62.  
  63. =====
  64.  
  65. Overflow (when sizes are too small)-- it loops back around since there's only 4 bits / 0 to 15
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement