-
October 30th, 2014, 01:14 AM
#1
Incorrect result in Bitwise operations
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.
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.
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 (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!
-
October 30th, 2014, 04:19 AM
#2
Re: Incorrect result in Bitwise operations
What is initial data[j] value?
Can you reduce this sample to single line of code, with hard-coded j value, which is supposed to give some result, but gives something else?
-
October 30th, 2014, 05:52 AM
#3
Re: Incorrect result in Bitwise operations
Without a working code sample to try I'm not sure - but have you tried casting to an unsigned int the result of (data[j]^0xb6) before doing the << 8?
ie (((unsigned int)(data[j]^0xb6)) << 8)
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 30th, 2014, 08:00 AM
#4
Re: Incorrect result in Bitwise operations
if result is 64, then by deduction.
data[j] was 0xb6 and data[j+1] was 0xeb
if result is 128, then data[j] was 0xb6 and data[j+1] was 0x2b
if result is 512, then data [j] was 0xb7 and data[j+1] was 0xab
...
It woudl appear you're just making wrong assumptions about what data was at a certain position.
or... your code sample doesn't set length to anything, so the value of length could be anything.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|