08 Sep, 2009, JohnnyStarr wrote in the 1st comment:
Votes: 0
The macro in ROM IS_SET() is defined as:

#define IS_SET(flag, bit) ((flag) & bit))


Since learning C++ (one step at a time), I've noticed that
it seems the only other way (besides a macro) would be to use a template function?
The reason I say that is because i've noticed some flags are <int> as where others are <long> etc.
If so, it is impressive that C has something like that to offer.

Secondary question:
When it comes to bitwise operators, I also find
it interesting that by just typing [ flag & bit ] it is able to tell if one of the bits are set.
Since I don't fully understand them yet, but it seems like it would require an algorithm
to check each bit and compare it? This observation is based on the fact that C is well known
for being "bare bones", providing you with very little.
08 Sep, 2009, Davion wrote in the 2nd comment:
Votes: 0
If you're going to do this in C++, I'd suggest checking out std::bitset. Great part of the STL to handle bits. If you want to know more about bitwise operators, just google them. Tons of stuff out there.
08 Sep, 2009, David Haley wrote in the 3rd comment:
Votes: 0
Quote
Since learning C++ (one step at a time), I've noticed that
it seems the only other way (besides a macro) would be to use a template function?
The reason I say that is because i've noticed some flags are <int> as where others are <long> etc.
If so, it is impressive that C has something like that to offer.

What exactly is your question here? The only other way to do what?
If there are several types of flags, you can use templates or overload the functions.

Quote
When it comes to bitwise operators, I also find
it interesting that by just typing [ flag & bit ] it is able to tell if one of the bits are set.
Since I don't fully understand them yet, but it seems like it would require an algorithm
to check each bit and compare it? This observation is based on the fact that C is well known
for being "bare bones", providing you with very little.

It's not using any algorithm beyond binary (boolean) arithmetic.
10101
& 01010
= 00000

10101
| 01010
= 11111

101
& 100
= 100

In other words, to test if the n^th bit is set, it suffices to 'and' the bitset with the number such that only bit n is set; if bit n is set in the bitset, the result will have the n^th bit set, meaning that it is non-zero and therefore tests to true.

Binary arithmetic like this happens to be extraordinarily fast; it's one of the cheapest possible instructions at a hardware level.

So basically this is a clever way of representing a set of boolean values. C isn't providing anything except for boolean arithmetic.


EDIT: and yes, I would very strongly recommend using an encapsulated data structure if you are working in C++. There's the STL bitset, or any number of other bitvector implementations.
08 Sep, 2009, Kline wrote in the 4th comment:
Votes: 0
One more vote for the bitset! No more being limited to "32" bits on x86 with bitwise operations! http://www.cplusplus.com/reference/stl/b...

bitset<256> my_bitset;
my_bitset.test(flag);
my_bitset.flip(flag);


etc … :)
0.0/4