This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Complement of Char

Is any direct function is available for the complement of the char variable

Parents Reply Children
  • 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.

  • 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
    

    Erik

  • 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.