Is any direct function is available for the complement of the char variable
A function to take the complement of a char. I even tested it with Keil.
char GetComplementOfAChar(char myChar) { return (~myChar); }
int main(void) { char myChar2;
myChar2 = GetComplemenOfAChar('a'); }
'a' = 0x61 return = 0x9e
"I even tested it with Keil."
And also with your tongue firmly in your cheek, surely...? ;-)
Oh, I think we can do better than that:
char GetComplementOfAChar (char c) { char result; int i; result = 0; for (i = 0; i < 8 * sizeof(c); ++i) { if (((c >> i) & 1) == 1) { result |= 0 << i; } else { result |= 1 << i; } } return result; } // GetComplementOfAChar
I got a better one. const char GetComplementOfAChar[256] = { 0xff, 0xfe, 0xfd, 0xfc, ...., 0x02, 0x01, 0x00 };
myChar = GetComplementOfAChar[0x02]; // returns 0xfd
Barry - he wants a function, so make that:
char GetComplementOfAChar( char c_in ) { char c_out; const char ComplementOfAChar[256] = { 0xff, 0xfe, 0xfd, 0xfc, ...., 0x02, 0x01, 0x00 }; c_out = ComplementOfAChar[ c_in ]; return c_out; }
Note This message was edited to reduce width.
View all questions in Keil forum