| Decimal | Binary |
|---|---|
| 0 | 0000 |
| 1 | 0001 |
| 2 | 0010 |
| 3 | 0011 |
| 4 | 0100 |
| 5 | 0101 |
| 6 | 0110 |
| 7 | 0111 |
| 8 | 1000 |
| 9 | 1001 |
| 10 | 1010 |
| 11 | 1011 |
| 12 | 1100 |
| 13 | ???? |
| 14 | ???? |
| 15 | ???? |
Can you guess what the last three binary codes are?
A Binary Adding Machine using marbles
Look for the video on this page:
[ http://woodgears.ca/marbleadd/ ]
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
add dick password
del dick
In the final I have given you programs like [ puzzle.cpp ] and [ 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 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
class Group {
private:
struct List { People * person; List * next; List * previous } list;
List * last;
...
for(People* p = list; p!=NULL; p=p->next ) ...
...
for(People* p = last; p!=NULL; p=p->previous ) ...
...
};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):
[ 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:
char a[10]; //an array of 10 characters.then a is the name of some storage.
The typedef version:
typedef char C10 [10];creates a new name that can only be used as a type:
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
vector < Person * > :: iterator p;and you might have a dozen for loops using this declaration...
So write
typedef vector < Person * > :: iterator VPI;and then you only have to type
for( VPI p = start; p!=end; p++)...
...
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: [ bitzi.cpp ] (print binary for small ints), and [ unfixed.misc.cpp ] [ 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
void *memcpy( void *s1, const void *s2, size_t n )and
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
[ 18.html ]
. . . . . . . . . ( end of section Computer Science II Session 17 Bits and Pieces) <<Contents | End>>
Abbreviations