unable to explain arbit behaviou in addition

Discussion in 'C' started by redgiant, Oct 9, 2006.

  1. redgiant

    redgiant New Member

    Joined:
    Oct 9, 2006
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    hi ,
    please refer to the code below:

    Code:
    //-----code follows----------------------------
    
    #include<stdio.h>
    
    typedef unsigned char UInt8;
    typedef unsigned short UInt16;
    typedef unsigned long UInt32;
    typedef unsigned long long UInt64;
    
    int main(){
       
        UInt16 lowP = 8874, lowC =61008;
        UInt16 midP = 32566, midC =32848;
        UInt8 hiP = 0, hiC =0;
    
       //following two lines produce wrong output, note that we dont type cast midP and midC here
        UInt64 valP = lowP + (midP << 16) + (((unsigned long long)(hiP))<<32); //line 1
        UInt64 valC = lowC + (midC << 16) + (((unsigned long long)(hiC))<<32); //line 2
    
        //uncommenting below two lines instead of above two lines gives right result
        
       // UInt64 valP = lowP + (((unsigned long)midP) << 16) + (((unsigned long long)(hiP))<<32);
        //UInt64 valC = lowC + (((unsigned long)midC) << 16) + (((unsigned long long)(hiC))<<32);
    
    
        UInt64 diff = valC - valP;
    
    
        printf("valP:%x\n",valP >> 32);
        printf("valC:%x\n",valC >> 32);
        
        return 0;
    }
    
    
    //----------------code end-----------------------------
    

    now when I run this program I get following output:
    valP:0
    valC:ffffffff

    However instead if I comment two lines mentioned as line1 and line2 and uncomment two lines
    following in the code above, I get the following output:
    valP:0
    valC:0

    Can somebody explain this behaviour? why this behaviour incase we dont typecast to unsigned long. This does not happen with any values but some values.
     
  2. redgiant

    redgiant New Member

    Joined:
    Oct 9, 2006
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    just to add I am using gcc from GNU for compiling above.
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Welcomte to G4EF.

    Use the code block for better formatting.

    Thats because when you shift 16 bits for some values it becomes zero but not for all and so you get
    the output unpredicted.
     
  4. redgiant

    redgiant New Member

    Joined:
    Oct 9, 2006
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Shifting by 16bits may give me zero but most significant 32 bits are ffffffff. In the code hiC corresponds to most significant 32 bits and value of hiC is 0. How by not typecasting midC, we are affecting most significant 32 bits?
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    If you dont type cast it becomes long by default but with type cast it become unsigned giving it an extra bit for positive numbers. Try type casting it to long and see if the result is same.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice