Some C coders really go nuts with macros. I’m not a big fan of those people.
Continue reading
Author Archives: dgookin
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
Bit Manipulation Program Demo Time
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