I recently asked a question about the following question:
Given a 32-bit number, write a pseudo code to translate the second last bit
What is the best / easiest way to do this?
#define MASK 0x00000002
new = old ^ MASK
I see that some answers interpret the "last bit" as MSB, others as LSB. Perhaps they are looking for candidates smart enough to pause and ask for clarification before twisting the code. This is very important in real work.
X ^ (1<<n) will toggle the state of nth bit in the number X.
2. , = ^ 2
a = 0x80000000; // the second last bit set if( i & a == 0) // not set in i -> set it i |= a; else // set -> un-set it in i i &= ~a;
edit: arg, , XOR :-) 2 - , . , MSB LSB.
XOR?