CSC161 2010F, Class 15: Pause for Breath Overview: * Thinking about assignment 4 Admin: * We will have a moment of silence at the beginning of class for the International Day of Peace. * Debriefing on Town Hall Discussions. * Shared governance vs. self governance * More authoritarian than expected * Is civility an issue for students? * What did he say self-governance is: We give you the right to make some decisions about yourselves in prepration for you becoming citizens. * In the evening session, it sounded like he felt it needed review, just like any other aspect of the College. * Reading for tomorrow: K&R 3.4. * Advance notice: Your class mentor will conduct a review session on Friday. * Are there questions on Assignment 4? Where do we begin on assignment 4? * In general * Put foam padding on your head before banging it against things * Break the problem into smaller parts * Look at things we've done in class * Look at recent readings and stuff for basic operations * For C like problems * Use man (may not be helpful for this particular assignment) * Look at K&R for similar examples * Part 1 * Problem: showintbits: Printing out the bits in an integer * Smaller parts: * Printing one bit * Printing the rightmost bit * One strategy (please don't use): Use modulus * (Printing any value) * Repeat it for each bit * Helpful knowledge: * Each bit corresponds to 2^kths power (rightmost is 2^0, next is 2^1, next is 2^2) * We know how to move bits left and right with <<, >> * Bitwise & can be useful 0 & 0 is 0 0 & 1 is 0 1 & 0 is 0 1 & 1 is 1 so 1 & anything is that thing so 0 & antyhing is 0 Start with the pattern 00000000000000000000000000000001 and bitwise and it with x, you get lots of 0s followed by the rightmost bit of x * Putting it together * Loop 32 times (for each bit), with k running from 31 down to 0 print the bit at position k * Part 1, continued * How do we print the rightmost bit? if ((i % 2) == 0) printf ("0"); else printf ("1"); * if ((i & 0x1) == 1) printf ("0"); else printf ("1"); * How does that help in our loop? After we print the current bit, we can shift the bit pattern left every time * Part 2 * Problem: * Smaller parts: * Helpful knowledge: