Ancalagon wrote:
If it's all ASCII values, there's only one bit difference between upper case and lower case, and it's the sixth bit.
So
char c;
if(isalpha(c))
c &= 0xdf;
should convert everything to uppercase ( use c |= 0x20 to convert to lowercase ).
That's kind of what I was doing, I would add 32 base 10 to convert from lowercase to uppercase and -33 base 10 to convert back.
32 base 10 (decimal) = 0x20 base 16 (hexadecimal) = 0010 0000 base 2 (binary)
likewise, -33 base 10 = 0xdf base 16 = 1101 1111 base 2.
But wouldn't you be subtracting by 32 instead of 33? -32 is actually 1110 0000 base 2, or 0xe0 base 16. See, when you converted 32 to negative you missed a step. Whenever you convert a binary # to it's negation, you have to flip the bits like you did, but then you have to add 1 (0000 0001). It goes like this:
0010 0000 (32)
1101 1111 (binary inverse of 32, actually -33)
+0000 0001 (1)
= 1110 0000 (1 + 1 = 10 in binary, keep the 0 and carry the 1 until you reach the 0 in the sixth bit. That becomes a 1, everything else remains the same)
It's called a two's complement. Basically, it helps when dealing with signed binary values to think of the last bit (in this case, the 8th bit) as a negative. So in the case of an 8 bit signed integer, the bits would have the following values:
-128 64 32 16 8 4 2 1
So whenever you have a negative number, the 8th bit is 1, then for all the other 1 bits, you take the value of the bit and add it to -128. Or, you can simply add the first 7 bits and subract 128.
So, for example, -33, or 1101 1111:
-128 + 64 + 16 + 8 + 4 + 2 + 1 = -64 + 31 = -33
= -128
_________________
"Yeah, so this one time, I tried playing poker with tarot cards... got a full house, and about four people died." ~ Unknown comedian
Happy New Year from WP's resident fortune-teller! May the cards be ever in your favor.