Hrm, I was browsing through the Base64 code for Fred, and I found something kind of odd. It does a bitwise and against an integer, but the mask is 0xFF. Meaning, it does absolutely nothing. (Unless I'm simply completely asleep. My weak point is bitwise operations, so..)

<snippet>
      if (i < in.length) val |= ((int)in[i++] & 0xFF) << 8;
      if (i < in.length) val |= ((int)in[i++] & 0xFF);
</snippet>

Couldn't that simply be:

<snippet>
      if (i < in.length) val |= ((int)in[i++]) << 8;
      if (i < in.length) val |= ((int)in[i++]);
</snippet>

My only thought is that an integer is 32 bits in Java, so it would mask everything but the lowest byte? We go from 1 byte, to an integer, then we pull the lowest byte?

Example (in pseudocode. 0b is my prefix for binary, ok?):

<snippet>
        in[x] = 0b11101110

        castedX = (int) in[x]
        // castedX is now == 00000000 00000000 00000000 11101110

        maskedCastedX = castedX & 0b11111111
        // maskedCastedX is now == 00000000 00000000 00000000 11101110
</snippet>

Now, it seems to me that the mask didn't do anything. The mast is internally cast up to an integer, making it equal to

00000000 00000000 00000000 11111111

However, since we cast a byte up to an integer, the high word and the high byte of the low word are going to be 0 anyways, so no information lost or changed by the mask. Am I going crazy?

It seems to me that the shift would come out the same way if we ommitted the mask. No sense making it hard to read and taking extra CPU cycles. The mask seems to do nothing, because you should get the same result even if you remove it altogether. It may be something to do with operator precendence, but I don't think so...

Excuse me if I made any mistakes. As I said, bitwise operations are *not* my strong point. If I *am* wrong, please explain why, so that I don't get stuck elsewhere. (Ian: That's why I was asking you about Bas64. It's not the concept, it's the bitwise operations that I suck at and I have to do them out on paper or something before I understand them.)

Aeloria

_______________________________________________
devl mailing list
[EMAIL PROTECTED]
http://hawk.freenetproject.org:8080/cgi-bin/mailman/listinfo/devl

Reply via email to