unsigned char xor4(unsigned char c1, unsigned char c2){
int i = 0;
while(i < 8){
if((getBit(c1, i) ^ getBit(c2, i)))
setBit(c1,i);
else clearBit(c1, i);
i+=2;
}
return c1;
}
The above code is supposed to be a very simple function to set every other bit in a character based on the result of an xor with every other bit in a second character. For some reason, this simply doesn’t work. The program seems to just ignore my while loop and return the original function.
Oh and here are my getBit, setBit and clearBit functions.
unsigned char getBit(unsigned char c, int n){
return (c & 1<<n) >> n;
}
unsigned char clearBit(unsigned char c, int n){
c = c & (~(1<<n));
}
unsigned char setBit(unsigned char c, int n){
`c = c | (1<<n);
}
You didn’t post your setbit and clearbit functions, but whatever they do, that can’t change c1 in the calling function. You need to either pass the address of c1 or return the new value of c1. Or you can just use C bitwise operators. And your entire operation is just equivalent to
c1 ^= c2(assuming that a char has 8 bits).Edit: Given your edit, just assign the return values of your functions to c1 … and fixe your setbit and clear bit functions … they don’t return values, and if you were using proper warning settings in your compiler it would tell you that. I’ve made some stylistic changes:
Here’s a faster solution:
Even faster: