.Open Computer Science II Session 17 Bits and Pieces (Previous): Linked Data Structures .See ./16.html . Preparation .Open All data is bits . Table of Binary numbers Here is a table of numbers in decimal and binary notation .Table Decimal Binary .Row 0 0000 .Row 1 0001 .Row 2 0010 .Row 3 0011 .Row 4 0100 .Row 5 0101 .Row 6 0110 .Row 7 0111 .Row 8 1000 .Row 9 1001 .Row 10 1010 .Row 11 1011 .Row 12 1100 .Row 13 ???? .Row 14 ???? .Row 15 ???? .Close.Table Can you can find a pattern in the above table. Can you guess what the last three binary codes are? . A Binary Adding Machine using marbles Look for the video on this page: .See http://woodgears.ca/marbleadd/ .Close . Chapter 21 and Appendix E.4 21.1 Introduction 1005 21.2 Structure Definitions 1005 A class with everything public by default. Useful to define the inner workings of a data structure in side a class. 21.3 Initializing Structures 1008 I didn't know this! Wow.... I could of used it. 21.4 Using Structures with Functions 1008 21.5 typedef 1008 Used to save typing, reduce errors. Also simple. 21.6 Example: High-Performance Card Shuffling and Dealing Simulation 1009 Line 49 has a subtle bug, I believe. 21.7 Bitwise Operators & | ~ ^ 1012 Fun. 21.8 Bit Fields 1021 Interesting but only useful to people writing device drivers... 21.9 Character-Handling Library 1025 cctype is a very useful library. 21.10 Pointer-Based String-Conversion Functions 1031 Very handy for converting X_to_Y. 21.11 Search Functions of the Pointer-Based String-Handling Library 1036 Dangerous. Not in CS202. 21.12 Memory Functions of the Pointer-Based String-Handling Library 1041 Be very careful with these. Only use when speed is vital to a project. 21.13 Wrap-Up 1046 Appendix E.4 1290 Important for command line interfaces. . The Arguments to Main -- Vital Tool for Command Line Interfaces See Appendix E.4 in the book. In the laboratory (lab09) you will need to understand how a program is given information on a command line. For example .As_is add dick password .As_is del dick In the final I have given you programs like .See ./puzzle.cpp and .See ./puzzle2.cpp to see if you can figure out what they do when compiled and run. . Assigned work -- study and submit a question on the above reading - Chapter 21 & Appendix E.4 . Chapter 21 pages 1005 -- Legacy C++ Code Both chapter 21 and the appendix section speak of working with legacy C and C++ code, do you think that this is important to learn and why? Yes. Absolutely. Most ( > 70% ) programming work is modifying existing programs -- so called "legacy code". Programs stay in use for decades. Someone has to fix them and improve them. This is called .Key maintenance and is a popular task to give to new hires in the computer business. Even when starting from scratch (so-called Green space projects) you may find that you are using and old version of language -- for example C rather than C++. Also -- in CSCI320 we study the history of languages and you again need to work with some pretty old stuff. This is the only way to understand why things are the way they are! So -- yes -- you need to know how old code works. . Chapter 21 pages 1006-1007 -- Struct Could you please go in more detail with "struct" because the book's explanations seems vague. See next. . Chapter 21 pages 1005... -- Structs What exactly is a struct? In modern terms -- a struct is a class with most of its members public. To be precise a struct defaults to public and a class defaults . Chapter 21 page 1005 structs Do structures have any modern use? Good question. You can get almost exactly the same effect as "struct...{...};" by writing "class...{public:...};". A common use is to declare a private struct inside a class to handle a linked data structure. Suppose I have a problem where I have a class of Group that needs an internal set of People (in the group) then I can write .As_is class Group { .As_is private: .As_is struct List { People * person; List * next; List * previous } list; .As_is List * last; .As_is ... .As_is for(People* p = list; p!=NULL; p=p->next ) ... .As_is ... .As_is for(People* p = last; p!=NULL; p=p->previous ) ... .As_is ... .As_is }; So I have a hidden (from my clients) internal double linked list.... Here is another example of creating an `ad hoc` structure -- it records then names of the months and the number of days in each month (ignoring leap years): .See ./struct.cpp Exercise: why am i not worried about making the structure of 'months' public? So structs are useful for quickly setting up a data structure. . Chapter 21 page 1008 structs and and parameter passing to functions What is the difference between passing structs to functions by value and passing structs to functions by reference? When you pass something by value a copy is made inside the function. If the structure is large and the function called many times this wastes time. When you pass something by reference the function is given the starting address and accesses the thing directly. If the structure is large this is much faster. Pass by reference also means that the function has access to the parts of the passed object and can do what it wants with them. This can be risky. Constant reference -- fast and safer. . Chapter 21 page 1008 typedef What does the text mean by saying that "...typedef provides a mechanism for creating synonyms (or aliases) for previously defined data types..."? If you declare a variable: .... variable ...... .As_is char a[10]; //an array of 10 characters. then `a` is the name of some storage. The typedef version: typedef .... typename ...... .As_is typedef char C10 [10]; creates a new name that can only be used as a type: .As_is C10 a, b, c; // 3 arrays of ten characters. . Chapter 21 page 1008 -- typedef How useful is typedef and can you show an example of how this can be useful? It's main function is to make code shorter. In the next two sessions you will meet the "iterators" in the STL. A typical declaration without typedef would be .As_is vector < Person * > :: iterator p; and you might have a dozen for loops using this declaration... So write .As_is typedef vector < Person * > :: iterator VPI; and then you only have to type .As_is for( VPI p = start; p!=end; p++)... .As_is ... .As_is for( VPI p = somewhere; p!=middle; p++)... and so on. By typing less you make fewer errors. And the code is easier to read as well as write. . Chapter 21 Page 1012 demo bitwise Can you demonstrate those bitwise operators? Can you go over the bitwise operators and how they are used? If we have time in class -- yes. Programs: .See ./bitzi.cpp (print binary for small ints), and .See ./unfixed.misc.cpp .See ./misc.cpp (struct, union, bits, bitfields, bitwise, and a do-while). . Would many of the operators be used for checking for correct data entries from users Not really. Mostly for checking the status of devices. . Chapter 21 Page 1021 - 21.8 Bit Fields Can you explain what Bit Fields are and how they work? The let you describe a detailed, bit by bit, a mapping of a piece of storage. Mostly used to handle hardware and character codes. . Chapter 21.8 1023 bit Fields why do bit fields with same data type takes less size than with mixed data types. Why not? The size of `int`, `char`, `double` is largely determined by the hardware. The size of a bit-field by the programmer. As a result bitfields can be slower than the normal data. The program has to use bit operations to extract bit fields. By the way -- I don't think you can have float or double bit-fields seeing the format and arithmetic is not defined. . Chapter 21 pages 1041 -- String-Handling Is there a performance difference between void functions .As_is void *memcpy( void *s1, const void *s2, size_t n ) and .As_is void *memmove( void *s1, const void *s2, size_t n ) ? I guess that memmove will be a little bit slower. Both are O(`n`) but the move won't be more than twice as long and the simple copy. Normally these are only used when you want to copy data quickly. The important thing is to NEVER use memcpy when the two memory areas overlap. The copy starts copying the data it has already copied. . Next -- STL Containers -- Data structures with fewer tears .See ./18.html .Close Computer Science II Session 17 Bits and Pieces