Hello there,
In the Arduino doc's they specify a type "unsigned long" which is a 32 bit unsigned number. They also state the max value is 2^32-1. This is pretty standard.
What happens though is when i multiply a number by another number and the result comes out to more than 24 bits the result is not correct and might even be somewhat random, or at least appear that way.
For all the following examples, we have Count declared as:
unsigned long Count;
and the analog return result (from analogRead) is 512 decimal.
What works for example:
Count=5000*4*analogRead(A0);
What does NOT work:
Count=5000*8*analogRead(A0);
I noted that the difference between these two examples is that the first does not result in something that requires 25 bits but the second does require 25 bits. That's one more than 24 bits so i thought 24 bits might be somehow the max we can use. This would be very strange if this is true so i did another test...
What else works fine:
Count=5000*4*analogRead(A0); //as before returns a successful result.
Count=Count+Count; //doubles the previous value which is what i wanted.
This achieves the desired result of 5000*8*analogRead(A0) and works just fine, even though it returns a result that requires at least 25 bits. This round about method works while the direct method does not work.
I never had this problem in 'regular' C on a 'regular' C compiler.
Any ideas about this?
In the Arduino doc's they specify a type "unsigned long" which is a 32 bit unsigned number. They also state the max value is 2^32-1. This is pretty standard.
What happens though is when i multiply a number by another number and the result comes out to more than 24 bits the result is not correct and might even be somewhat random, or at least appear that way.
For all the following examples, we have Count declared as:
unsigned long Count;
and the analog return result (from analogRead) is 512 decimal.
What works for example:
Count=5000*4*analogRead(A0);
What does NOT work:
Count=5000*8*analogRead(A0);
I noted that the difference between these two examples is that the first does not result in something that requires 25 bits but the second does require 25 bits. That's one more than 24 bits so i thought 24 bits might be somehow the max we can use. This would be very strange if this is true so i did another test...
What else works fine:
Count=5000*4*analogRead(A0); //as before returns a successful result.
Count=Count+Count; //doubles the previous value which is what i wanted.
This achieves the desired result of 5000*8*analogRead(A0) and works just fine, even though it returns a result that requires at least 25 bits. This round about method works while the direct method does not work.
I never had this problem in 'regular' C on a 'regular' C compiler.
Any ideas about this?