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
Author Archives: dgookin
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
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