To assemble all the bit field manipulation functions into one program, I present the status portion of what could be a complex computer game. The bit fields represent the current conditions of the game’s starship. The fields are adjusted as the game progresses and various things happen.
Continue reading
Reading Bits and Resetting Bits
Once you break out the tools, bit manipulation in C can be a fascinating and useful thing. Setting a bit, covered in last week’s Lesson, is a big first step. Once you’ve done that, the functions to read and set a bit fall into place naturally.
Continue reading
Read a Percentage Value – Solution
One of the things I enjoy about coding defensive input is that I get to think about all the oddball possibilities. Users can type anything, which is something you must always anticipate. For typing in a percentage value, the door for whacky input is left wide open.
Continue reading
Bit Field Manipulation
The three basic bit manipulation operations are:
- Set a bit (change its value to 1)
- Reset a bit (change its value to 0)
- Read a bit (determine whether it’s 1 or 0)
The standard C library lacks specific functions to carry out these bit manipulations, and I haven’t checked to see whether a third party library is available. That’s because you can easily code these operations on your own.
Continue reading
Read a Percentage Value
Here’s a thought experiment for you: How can you prompt a user to properly input a percentage value?
Continue reading
The Joy of Bit Fields
One of the C language’s strengths is that it can venture down into the depths of a computer’s inner workings. It loves to play in those dark, subterranean pools of raw data, bits, ports, and other primitive places that few high-level languages dare to tread.
Continue reading
A Recursive Permutation Function
Of course recursion is scary! At no other time in your programming career is your code more susceptible to stack overflows, which appear on the screen as ugly system errors when the code runs. I find that exciting.
Continue reading
Recursive Permutations
Recursion is a type of programming loop, one that’s deliberately designed to drive some programmers nuts. It’s also a great way to replace nested loops, such as those used in last week’s Lesson.
Continue reading
Appreciation for Depreciation – Solution
This month’s Exercise is one of those old, programming warhorses: Calculate a depreciation schedule. At the simplest level, the solution is a mathematical loop and keeps subtracting a given percentage from a value.
Continue reading
Brute Force Permutations
If Arthur C. Clarke’s story The Nine Billion Names of God were true, then it seems rather pointless to plow through all 9,000,000,000 permutations to find one matching name. In fact, the exercise is more like a brute-force password cracking program than some celestial name search. What if the monks already knew the name? That would save time and effort.
Continue reading