Copyright 2004 by M. Uli Kusterer Tue, 30 Nov -1901 08:00:00 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 10 by Uli Kusterer http://masters-of-the-void.com/book14.htm#comment10 http://masters-of-the-void.com/book14.htm#comment10 Uli Kusterer writes:
Andre,

You should use strlen() instead of sizeof(). sizeof() measures the size of a type or local variable, which won't work for these parameters. sizeof(argv[1]) is always 4, because it is the size of a pointer variable (and arrays are just pointers). strlen will look at each character until it encounters a character with value 0, and then it will give you the number of characters it skipped.
Comment 9 by Andre http://masters-of-the-void.com/book14.htm#comment9 http://masters-of-the-void.com/book14.htm#comment9 Andre writes:
Me again!
I was tempted to realize the primitive XOR encryption and wrote this little tool. You need to run it with one parameter to tell it what term to encrypt. (run it from the command line)
The DECRYPTED printf is just a mere proof of concept.
This is actually my first C program I wrote on my own. Hope everything is correct.

-

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (int argc, const char * argv[])
{
char key = '#';

char encrypted[sizeof(argv[1])];
char decrypted[sizeof(encrypted)];

int x = 0;
for (x = 0; x < sizeof(argv[1]); x++) {
encrypted[x] = argv[1][x] ^ key;
}

for (x = 0; x < sizeof(encrypted); x++) {
decrypted[x] = encrypted[x] ^ key;
}

printf("ORIGINAL %sn", argv[1]);
printf("ENCRYPTED %sn", encrypted);
printf("DECRYPTED %sn", decrypted);

return 0;
}
Comment 8 by Dennis http://masters-of-the-void.com/book14.htm#comment8 http://masters-of-the-void.com/book14.htm#comment8 Typo?

The let shift operator is handy for generating a bit mask...

should be:
The left shift operator is handy for generating a bit mask...

yes?

P.S. Thank you for the great work you're doing here!
Comment 7 by Imran Khan http://masters-of-the-void.com/book14.htm#comment7 http://masters-of-the-void.com/book14.htm#comment7 Uli Kusterer, I thank you so much this tutorial. It gives me confidence to work on Xcode in C language. I was desperate before that how to use Xcode for only C. The other thing is that your tutorial revised me a lot of things that I had forgotten so long ago.
Thanks a lot !
Would you please prepare for Objective-C tutorial like this one? I will be happy to benefit it from you.
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.