+ Post New Thread
Results 1 to 6 of 6
  1. #1
    Full Member level 6
    Points: 3,140, Level: 13
    Achievements:
    7 years registered

    Join Date
    Jul 2010
    Location
    Germany
    Posts
    334
    Helped
    58 / 58
    Points
    3,140
    Level
    13

    AVR studio doubt wrong result

    Can anyone please explain why the result of the operation given below is always wrong

    unsigned long var = 0;

    var = 10*9600;

    The result comes as 30464. I have even tried by changing the data type of "var" to double.


    Waiting for a solution...
    "I have no special talents. I am only passionately curious." Albert Einstein

    Regards,
    Embedded_Geek

    •   AltAdvertisment

        
       

  2. #2
    Advanced Member level 4
    Points: 6,778, Level: 19
    Achievements:
    Created Blog entry Created Album pictures 7 years registered
    alexxx's Avatar
    Join Date
    Apr 2011
    Location
    Greece
    Posts
    1,011
    Helped
    278 / 278
    Points
    6,778
    Level
    19
    Blog Entries
    2

    Re: AVR studio doubt wrong result

    Quote Originally Posted by Embedded_Geek
    Can anyone please explain why the result of the operation given below is always wrong

    unsigned long var = 0;

    var = 10*9600;

    The result comes as 30464.
    Try this:

    Code:
    var = 10ul*9600ul;
    That should work.


    1 members found this post helpful.

    •   AltAdvertisment

        
       

  3. #3
    Full Member level 6
    Points: 3,140, Level: 13
    Achievements:
    7 years registered

    Join Date
    Jul 2010
    Location
    Germany
    Posts
    334
    Helped
    58 / 58
    Points
    3,140
    Level
    13

    Re: AVR studio doubt wrong result

    Thank you very much alexxx. It worked fine as you said but can you explain me why 10*9600 is not working. I mean I have used this kind of statements in IAR, RIDE etc.
    "I have no special talents. I am only passionately curious." Albert Einstein

    Regards,
    Embedded_Geek



    •   AltAdvertisment

        
       

  4. #4
    Advanced Member level 4
    Points: 6,778, Level: 19
    Achievements:
    Created Blog entry Created Album pictures 7 years registered
    alexxx's Avatar
    Join Date
    Apr 2011
    Location
    Greece
    Posts
    1,011
    Helped
    278 / 278
    Points
    6,778
    Level
    19
    Blog Entries
    2

    Re: AVR studio doubt wrong result

    Quote Originally Posted by Embedded_Geek View Post
    can you explain me why 10*9600 is not working. I mean I have used this kind of statements in IAR, RIDE etc.
    I also do not have such problems in IAR. GCC is an older and more "strict" compiler. The problem is that when the compiler sees raw numbers < 65536, then it interprets them as 16-bit.
    So 10*9600 = 96000 = 0x17700.
    But since this result is interpreted as 16-bit, then the remaining result will be 0x7700 = 30464. You have to use this "ul" to declare that this is an unsigned long value.

    Depending on the value you can use ui for 16-bit, ul for 32-bit and ull for 64-bit values.

    Alexandros


    1 members found this post helpful.

  5. #5
    Full Member level 6
    Points: 3,140, Level: 13
    Achievements:
    7 years registered

    Join Date
    Jul 2010
    Location
    Germany
    Posts
    334
    Helped
    58 / 58
    Points
    3,140
    Level
    13

    Re: AVR studio doubt wrong result

    Where can I find this kind of restriction regarding gcc?
    "I have no special talents. I am only passionately curious." Albert Einstein

    Regards,
    Embedded_Geek



    •   AltAdvertisment

        
       

  6. #6
    Advanced Member level 4
    Points: 6,778, Level: 19
    Achievements:
    Created Blog entry Created Album pictures 7 years registered
    alexxx's Avatar
    Join Date
    Apr 2011
    Location
    Greece
    Posts
    1,011
    Helped
    278 / 278
    Points
    6,778
    Level
    19
    Blog Entries
    2

    Re: AVR studio doubt wrong result

    Quote Originally Posted by Embedded_Geek View Post
    Where can I find this kind of restriction regarding gcc?
    Try google with "integer constant suffix" as keywords. This is not only a GCC issue but a C issue in general.
    I've googled a couple of references to get you started:

    http://msdn.microsoft.com/en-us/library/2k2xf226.aspx
    http://www.delorie.com/gnu/docs/gcc/cpp_48.html

    About the second reference, look at the bottom of the page.


    1 members found this post helpful.

--[[ ]]--