Copyright 2004 by M. Uli Kusterer Tue, 30 Dec 1969 07:58:58 GMT Comments on article book14 at Zathras.de http://www.zathras.de/angelweb/book14.htm book14 Comments witness_dot_of_dot_teachtext_at_gmx_dot_net (M. Uli Kusterer) witness_dot_of_dot_teachtext_at_gmx_dot_net (M. Uli Kusterer) en-us Comment 6 by Uli Kusterer http://masters-of-the-void.com/book14.htm#comment6 http://masters-of-the-void.com/book14.htm#comment6 Uli Kusterer writes:
Amoo, you are correct, (1 << 0) evaluates to 1. I just find it tidier to have all the bits written the same way. You can scan over the code more easily, and see that bit 0, 1, 2, ... etc. are defined here.
Comment 5 by Amoo http://masters-of-the-void.com/book14.htm#comment5 http://masters-of-the-void.com/book14.htm#comment5 Hi Uli,

As I understood, #define FLAG_ONE (1 << 0) does not shift 1 at all. So why not writing:

#define FLAG_ONE 1

is using the shift operator just for the sake of clarity?
Comment 4 by Uli Kusterer http://masters-of-the-void.com/book14.htm#comment4 http://masters-of-the-void.com/book14.htm#comment4 Uli Kusterer writes:
Scott,

as the text mentions, when you need a bunch of booleans to give to a function, it's much more convenient. Particularly if they're related. Also, if you're trying to select one or more items from a list, based on their type, you can use one bit to represent each type. You can only have 32 types, but if you want to find items matching one *or* the other type, you just OR together these two types into a 'mask', then loop over the items and AND the mask with the current item's type. If the result is not zero, you have a match. Pack that in a utility function and you have a quite efficient search.

Colors can easily be represented as a struct, so colors themselves aren't that interesting. Where it becomes interesting is when you want to represent lots of colored pixels. Though essentially you're doing a simple kind of compression then. You're storing four smaller numbers in one larger int.
Comment 3 by Scott http://masters-of-the-void.com/book14.htm#comment3 http://masters-of-the-void.com/book14.htm#comment3 This tutorial is really great Uli. You mention a few examples of when messing with the binary might prove useful, but it is my understanding that one is typically better-off using higher-level abstraction whenever possible. I've read that bitwise operators are mostly seen in code that works with colors, is this true? So besides compression, light-duty encryption, and possibly colors, when would it make sense to use such a low-level approach (is this sometimes done for optimization purposes)?
Comment 2 by Uli Kusterer http://masters-of-the-void.com/book14.htm#comment2 http://masters-of-the-void.com/book14.htm#comment2 Uli Kusterer writes:
Thanks Josh, fixed it.
Comment 1 by Josh http://masters-of-the-void.com/book14.htm#comment1 http://masters-of-the-void.com/book14.htm#comment1 Josh writes:
Errata:

But most computers deal in batches of 8, 6 or 32 bits at all times.

6 should be 16.

Thanks again for the great tutorials.