Thread: Incorrect result in Bitwise operations

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    8

    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.

    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!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Run the code in the debugger.

    Put a breakpoint on line 11 (or whatever is inside the real if ( result > 0 )

    Run the code, wait for the breakpoint, then examine each participating variable in detail.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. incorrect result in program
    By johnmerlino in forum C Programming
    Replies: 2
    Last Post: 02-15-2014, 08:46 PM
  2. Bitwise Operations Help Please!
    By workisnotfun in forum C Programming
    Replies: 2
    Last Post: 11-19-2012, 10:22 AM
  3. Bitwise operations
    By sh3rpa in forum C++ Programming
    Replies: 16
    Last Post: 09-25-2007, 06:32 PM
  4. Incorrect result with my program
    By JoshR in forum C++ Programming
    Replies: 4
    Last Post: 04-27-2005, 03:46 PM
  5. bitwise operations in C
    By samps005 in forum C Programming
    Replies: 5
    Last Post: 10-10-2002, 03:35 PM