This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

XOR Operation for byte

I write a^= 0XFF; then realize it perform xor for bit-7 not with all.

Suggest a XOR for 8-Bit not single bit.

--------

Vikas Dabas

  • vikas dabas said:
    I write a^= 0XFF; then realize it perform xor for bit-7 not with all

    Something wrong there!

    ^ certainly should be a bitwise operator in 'C'!!

  • Andy Neil said:
    Something wrong there!

    Indeed. Right away I would code this to check in debugger that result is indeed 0x00:

    unsigned char a = 0xFF;
    a ^= 0xFF;
    while (1) ;

    If not, then blame compiler manufacturer. Otherwise myself ;)

  • To XOR the whole byte with 0xFF is the same as: a = ~a;

  • Brian Boorman said:
    To XOR the whole byte with 0xFF is the same as: a = ~a;

    Correct. And 1+1 = 2. So what?

    Did you read the thread to know what actually we are talking about?

  • Ilmars said:
    Did you read the thread to know what actually we are talking about?

    Thanks for the snarky comment. And to answer your question: Yes I did. Did you?

    Vikas Dabas said:
    Suggest a XOR for 8-Bit not single bit.

    How does my answer not solve the EXACT question he asked, in the context in which he asked it (i.e. 0xFF)????

  • Thanks for complements.

    Now take it in another way, I used Gray to Binary conversion.

    I find this the best way is  XOR operation. 0XFF is not fixed it changes with Gray code input

    If possible then suggest other way like XOR i.e. simple n sweet.

    llmars:

    unsigned char a = 0xFF;                                                        
    a ^= 0xFF;

    I think it is still bitwise operation performing here. Am i right??? or it is solution.

    --------

    Vikas Dabas

  • Vikas,

    a ^= 0xFF;

    should invert all 8 bits of a (assuming a is an 8-bit variable, such as "unsigned char").

    If that is not what you are seeing then you have a compiler issue.

    Another option, if this is just some test code, and a doesn't get used somewhere else, then maybe the compiler is skipping the instruction as part of optimization. Make sure you turn off all optimizations (at least initially) when developing your code.

    Which compiler (and version) and OS are you on. Can you provide a concise test case (project in a zip file) showing just the error?

  • Brian Boorman said:

    Suggest a XOR for 8-Bit not single bit.

    How does my answer not solve the EXACT question he asked, in the context in which he asked it (i.e. 0xFF)????

    [/quote]

    Yes, 0xFF was mentioned as an example but anyway he did not ask "suggest XOR using constant 0xFF value as second operand". So I was suggesting determined test he can make to check that XOR operation indeed does not work correctly for given (unspecified) C compiler.

  • vikas dabas said:
    I think it is still bitwise operation performing here. Am i right??? or it is solution.

    This is test you shall make and report results. I asked such way to exclude any possible coding error of yours but to check subject of your question, and only. Did you check? What was result?

**Attention** This is a public forum