Thread: incorrect result in program

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    62

    incorrect result in program

    I have this simple program below:

    Code:
    #include <stdio.h>
    #include <limits.h>
    
    unsigned int rightrot(unsigned int x, unsigned int n)
    
    {
    
       /* calculate number of bits in type */
    
       size_t s = sizeof(x) * CHAR_BIT;
    
       size_t p;
    
    
    
       /* limit shift to range 0 - (s - 1) */
    
       if(n < s)
    
           p = n; 
    
       else
    
           p = n % s;
    
    
    
       /* if either is zero then the original value is unchanged */
    
       if((0 == x) || (0 == p))
    
           return x;
    
    
    
       return (x >> p) | (x << (s - p));
    
    }
    
    
    
    
    
    int main(void)
    
    {
    
       unsigned int val;
    
       unsigned int pos;
    
       val = 0xFF94;
       pos = 5;
    
       printf("%u\n", rightrot(val, pos));
    
    }
    The result it prints is 2684356604 on my 32-bit computer. The result I expect is as follows:

    0xFF94 is 0000000000000000 1111111110010100 in binary.

    Shift it right by 5:
    0000000000000000 0000011111111100

    Then take that result in shift it right by 27 (s is 32 and p is 5, so the difference is 27):
    1111111110000000 0000000000000000

    Now we use bitwise or:
    0000000000000000 0000011111111100 | 1111111110000000 0000000000000000 = 1111111110000000 0000011111111100

    That in decimal is 4286580732. So how does it come up with 2684356604?

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    One way to find such errors is to translate your english into asserts and make sure it is working as you assumed. Execution will stop on your first incorrect statement

    Code:
    ...
        /* if either is zero then the original value is unchanged */
        if((0 == x) || (0 == p)) 
            return x;
    
        unsigned lhs = x >> p;
        unsigned rhs = x << (s - p);
        unsigned res = lhs | rhs;
        if (x == 0xFF94 && p == 5) {
            // (Shift it right by 5) lhs should be 0000 0000 0000 0000 0000 0111 1111 1100
            assert(lhs == 0x000007FC);
            // s - p should be 27
            assert(s - p == 27);
            // (Shift it left by 27) rhs should be 1111 1111 1000 0000 0000 0000 0000 0000
            assert(rhs == 0xFF800000);
            // (Now we use bitwise or) res should be 1111 1111 1000 0000 0000 0111 1111 1100
            assert(res == 0xFF8007FC);
        }
        return res;
    }

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Surely you want to isolate the bits being shifted out (with a bitwise &) and only | those with the intermediate result? (1U << n) - 1 is probably your mask

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple program weird result
    By nav301 in forum C Programming
    Replies: 3
    Last Post: 12-20-2006, 02:07 PM
  2. Replies: 2
    Last Post: 08-29-2006, 08:58 AM
  3. Search-result-opening program
    By Verdagon in forum C++ Programming
    Replies: 3
    Last Post: 08-02-2005, 05:41 PM
  4. Incorrect result with my program
    By JoshR in forum C++ Programming
    Replies: 4
    Last Post: 04-27-2005, 03:46 PM
  5. strange result of after compiling a program
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 04-23-2002, 09:54 AM