Today I was working on the students who in college was struggling with being able to read a project spec or a project problem set and knowing where to go and tackle the problem. So what I coach them on doing is rather than focusing on the larger problem sets, try to break down the problem to more concrete examples.
In this particular assignment, his task is to read a binary file which has integers stored in “big-endian” format and print out the numbers in hexadecimal and decimal and break down the bytes of the integer in the 4 bytes and printing those out either as printable characters or as the actual number in hexadecimal
As a tutor, my value add is to be able to tailor a lesson specific to where to student is at. Not too easy and especially not too hard.
He seem to be understand some of the basics of numbers, hexadecimal, C syntax.
I started to just have him hardcode a number based on the example from the assignment. Then asked him to use printf(“0x8X”, num); to print the number in hex and printf(“0x8u”, num) to print the number in decimal.
Then had him create another unsigned in to represent byte1 and hardcoded it and printing it. Then byte2, hard coding it. He was comfortable here.
I asked him to print out byte1 as if isprintable() and print out the decimal value if not.
While this all hardcoded, I told him it will build up to something more later.
Next is to read all the integer from the file, using a while loop and fread();
Once the read loop works, he can replace the hardcoded num with what is being read from the file in the loop.
He can see how the solution is built from bottoms up.
Next up, to extract the 4 bytes from the integer. I asked him if they learned bitwise operators. He immediately understood that the ‘&’ operator should be used, but wasn’t sure whether to use 0xF or 0xFF. I asked him about number of bits in 0xF, he was really sure. Instead of pushing him to think hard about the number of bits, I did relent and asked him given an example integer, 0xA3, which mask we should use, by really just visually looking at it.
He as a little confused about using shift right or left. So I had him write down the number in a concrete format.
0x34 A2 BB F1 and asked him to visually moving these number like a ticker tape to the right. Which became 0x00 34 A2 BB, one more time to be come 0x00 00 34 A2.. etc. He immediately figure the following
byte1 = num & 0xff;
byte2 = (num >> 8) & 0xff;
byte3 = (num >> 16) & 0xff;
byte4 = (num >> 24) & 0xff;
Today, as a tutor, I hope to have gotten better at helping a student understand how to come up with a solution to a complex problem by using concrete examples instead of thinking of the abstract.