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
that should send the OP spinning. He got what he wanted "a function to ..." and now you give him some totally silly code responding to the letter of the request.
Is the purpose of the forum that when someone ask "how do I do this in a stupid way" to "help" him/her forever code such in a most inefficient way. Or is it REAL help to post It's an operator, not a function. The operator is ~ as Dan did and leave it at that.
Anyone working for me that did a complement as a function would at the very minimum be told "no raise at next review".
Erik
"no raise at next review"
Unless, of course, he works somewhere where they pay per line of code...
"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
I'm sure you could've slipped a ternary operator in there somewhere, Drew...?
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.
Unless, of course, he works somewhere where they pay per line of code...<p>
Hm, I think they have missed that in the Guide to Writing Unmaintainable Code.
"Replace basic operators with self-written functions."
I'm sure replacing some binary operators with functions that simulate the truth tables would be great fun and extremely educational.
""Replace basic operators with self-written functions."
And hide them in macros, of course...!
;-)
the OP mentioned "this is about a bit" so, let us make it about a bit and we'll earn money when being paid per line of code:
U8 ComplementChar(U8 Phred) { if (Phred & 0x01) { Phred &= 0xfe; } else { Phred |= 0x01; } if (Phred & 0x02) { Phred &= 0xfd; } else { Phred |= 0x02; } if (Phred & 0x04) { Phred &= 0xfb; } else { Phred |= 0x04; } if (Phred & 0x08) { Phred &= 0xf7; } else { Phred |= 0x08; } if (Phred & 0x10) { Phred &= 0xef; } else { Phred |= 0x10; } if (Phred & 0x20) { Phred &= 0xdf; } else { Phred |= 0x20; } if (Phred & 0x40) { Phred &= 0xbf; } else { Phred |= 0x40; } if (Phred & 0x80) { Phred &= 0x7f; } else { Phred |= 0x80; } } // end ComplementChar
Are you sure the OP meant complement and not compliment? Complimenting a charcter would change the whole dynamic of the function and might make a lot more sense. For example:
char *GetComplimentOfAChar (char c) { if (c < 0) return ("You look very pretty today."); if (c < 64) return ("My, you're a handsome character."); return ("My, what a big character you are."); }
:-)
Jon
the OP mentioned "this is about a bit"
Maybe a function that copies a char to a bdata variable and then inverts each bit separately ? Might be fun.
Hm, maybe the OP can try this:
sbit ACC_0 = ACC^0; unsigned char getComplement(unsigned char input) { register unsigned char i = 8; ACC = input; do { ACC *= 2; ACC_0 = CY ? 0 : 1; } while(--i); return ACC; }
warning: the code above is strictly non-ansi and non-portable.