When I code a program, I start out by slapping together the various elements. I setup the variables, I write some quick routines, and I add comments to the tune of /* Do something here*/. With those bricks in place, I go back and fill in the mortar to make it all work. If the code runs, great! That rarely happens, so more work is involved.
Continue reading
Find the Best Size Container
Not everything in the real world appreciates the holy computer numbers. These are binary values that parallel the powers of 2: 1, 2, 4, 8, 16, 32, 64, and so on.
A common computer puzzle is how to allocate storage specific to those holy numbers, especially when the sizes of items that you’re working with don’t exactly line up to a specific holy computer number.
Continue reading
The Problem of 100 Doors
Plenty of interesting and fun programming puzzles are available to test your skills. Some of these puzzles come from the realm of mathematics or logic. What those propellerheads do with the solutions is up to them, but often you can code such problems to help you learn more about programming. One such logic problem is called 100 Doors.
Continue reading
Macros with Variables
Some C coders really go nuts with macros. I’m not a big fan of those people.
Continue reading
User-Defined Functions and Macros
When you desire to do something specific in your program, or have a chunk of code that’s duplicated elsewhere, you craft a function. That’s the traditional, problem-solving approach, and the C language is full of functions. Yet sometimes, programmers build macros instead of functions.
Continue reading
Off to the Races – Solution
The purpose of the winner() function in the horse race game is to determine which value in an integer array is the largest. That would be an easy exercise to write, but it lacks the thrill and excitement of a horse race.
Continue reading
Writing a Wide Bit Field
Writing a chunk of bits into an integer at a specific position requires major application of binary manipulation. That sounds impressive and mystical. To put it another way, writing a bit field is like using a stencil to paint a wall: You create the stencil, set it in place, then you slap down the paint.
Continue reading
Off to the Races!
Computer games were simple back in the early days. Output was printed on a teletype or displayed on a text-only CRT. Input wasn’t interactive or real-time. These games were fun to play back then, but are kind of lame now. They still exist with regards to simple programming exercises. In fact, you can pound out a older type computer game in a few minutes if you know the basics of the C language.
Continue reading
Reading a Wide Bit Field
The same programming kung fu used to read a single bit also applies to wide bit fields. The process involves masking and shifting: You must know the bit field’s width and position in an integer value. Once those values are obtained — no matter how wide the bit field — a single function handles the job.
Continue reading
Wide Bit Fields
An on-off bit field is pretty common; you’ll find them all over. Also common are wide bit fields, which can hold values greater than one or zero. These wide bit fields are often mixed with single-bit fields making for a wonderful stew of binary data all held within a single integer value.
Continue reading