We’ll cover two bit related topics in this post: getting/setting bits directly within an integer and working with bit fields in a C structure. Both have their place, it’s more about the context in which you need to use them.
The first example will be getting/setting of specific bits using an integer value along with a pre-defined set of bits we want to access. From there we will look at creating a structure that has named bit fields, making access (and code readability) much easier.
I previously wrote about C ternary operatar which provides an opportunity to write code that is “short-hand” if you will. A ternary operator is one that accepts three arguments, more on this below.
This is the traditional form of an if/else:
int x = 5;
if (x > 1)
y = x;
y = -1;
And here is a version using the ternary operators:
With an understanding of C structures, and the definitions of CGRect, CGSize and CGPoint behind us, let’s look at a handful of functions for working with these structures.
In this third article on C++ programming on the iPhone platform, I will be covering RTTI, or Run Time Type Identification. This C++ language feature is often unsupported on mobile platforms due to the usual "code bloat" reasons. So, let’s take a look at the iPhone and see what it has to offer.
Digging into development of iPhone applications, you’ll eventually encounter references to CGRect, CGSize, and CGPoint. These references are to C structures (see this post for more information on structures). This post will provide a high-level view of what comprises CGRect and its counterparts. Here is how CGRect is defined:
Leading up to a post on working with CGRect, CGPoint and CGSize, it makes sense to visit C structures. A structure is a collection of variables, grouped together to facilitate organization of data. For example, one might define a set of x and y coordinates as follows:
In part 2 of this C++ on iPhone series I’ll be exploring C++ exception handling support, and as a bonus I’ll touch on use of standard C++ lib console output stream, as well as showing a way to call C++ code from Objective C. As a reminder, exception handling is normally one of the weak spots in "mobile device" C++ support, so I wanted to find out how well it is supported on iPhone.
Jumping right into some code, using the same Xcode project as in part 1, I added a new C++ class
Exceptions.h to the project).