Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

AVR studio doubt wrong result

Status
Not open for further replies.

Embedded_Geek

Full Member level 6
Joined
Jul 5, 2010
Messages
340
Helped
58
Reputation
116
Reaction score
56
Trophy points
1,318
Location
Germany
Activity points
2,948
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...
 

Embedded_Geek said:
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.
 
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.
 

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
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top