Hi all,
I'm doing a bitwise operations on 2 bytes in a buffer, then storing the result in a variable. However, I sometimes get a non-zero value for the variable even though I'm expecting a zero value.
I'm sorry for not being able to post the whole code, but the code is too long for me to do that.
The relevant portion of the code is as follows.
Code:
unsigned int result = 0;
long j = 0, length;
unsigned char *data;
data = (unsigned char *)malloc(sizeof(unsigned char)*800000);
// populate data[]
while (j < length)
{
result = (unsigned int)(((data[j]^0xb6)<<8)|(data[j+1]^0xab));
if (result > 0)
...
(j gets incremented here)
}
I'm expecting result to be zero when my data[j] and data[j+1] are 0xb6 and 0xab respectively, which is the case for most of the time. However, for certain values of j, my result is strangely not zero.
Code:
j = 62910, result = 64
j = 78670, result = 64
j = 100594, result = 64
j = 165658, result = 512
j = 247990, result = 128
j = 268330, result = 512
j = 326754, result = 1
j = 415874, result = 256
j = 456654, result = 1024
j = 477366, result = 512
It appears that these strange result values are all powers of 2, with a 1 bit appearing somewhere in the unsigned int.
I'm not changing the value of result anywhere else in the code, and when I print out
Code:
(unsigned int)(((data[j]^0xb6)<<8)|(data[j+1]^0xab))
I get 0, but somehow when it gets stored in result, it's no longer zero.
I really don't understand what could be wrong.
Please help. Thanks!