Some C coders really go nuts with macros. I’m not a big fan of those people.
Continue reading
Category Archives: Lesson
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
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
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
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
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