You can think of modulus as counting to the number and then resetting to 0 when it reaches it.
so 15 % 10 we count up from 0 until we reach 10, at that point we reset our counter to 0 again and continue counting. The end result being 5.
0 = 0
1 = 1
2 = 2
3 = 3
4 = 4
5 = 5
6 = 6
7 = 7
8 = 8
9 = 9
10 = 0
11 = 1
12 = 2
13 = 3
14 = 4
15 = 5
Result = 5
On the other hand 15 % 2 = 1 because 15 is an odd number.
0 = 0
1 = 1
2 = 0
3 = 1
7 % 3 = 1 because we reset the counter to 0 every time the counter reaches 3.
0 = 0
1 = 1
2 = 2
3 = 0
4 = 1
5 = 2
6 = 0
7 = 1
Code: Select all
The above example is about how compiler calculate the 123%10 or is it a general example as you stated Tempresult1 and Tempresult2.
The compiler does all the heavy lifting for you behind the scenes, I was just highlighting what it does for you and hence why the % or MOD function is sometimes frowned upon on time critical applications.
For example 16 % 2 is far less efficient then doing 16 & 1 but both produce the same result. Of course not all calculations can be reduced to simple binary AND functions.
Here are some examples that do work and will be a lot more efficient.
x % 2 is the same as x & 1
x % 4 is the same as x & 3
x % 8 is the same as x & 7
x % 16 is the same as x & 15
On a microcontroller real numbers (numbers with a decimal point) are generally special case and have to be done using special Floating point variables.
800 / 1000 in integer maths is 0, 0 + 48 = '0' or 48 e.g. the ASCII value for 0.http://www.asciitable.com/
Having a single ASCII character between single quotes like that is the same as writing the decimal number. e.g. '1' = 49.
How does the Compiler recognise the Number as ASCII is it by Default or is there a pointer?
To the compiler a number is a number there is no difference. '0' = 48 = 0x30 = 0b00110000. They are all treated in exactly the same way. 0x stands for hexadecimal and 0b stands for binary.
To prove this try passing the different values above to the PrintASCII LCD function. They should all end up showing you an ASCII '0'.
I have noticed in some Datasheets, the i2c communication some Manufacturers specifically stating LSB or MSB to be sent first .
A INT type variable is made up of two bytes, a high byte and a low byte. To split the INT into bytes you would do this.
MSB = INT >> 8
LSB = INT
You can then choose which order to send the bytes.
To recombine the bytes into an INT you would do something like this.
INT = (MSB << 8 ) + LSB
More info and worked examples on this here.http://www.matrixtsl.com/article.php?a=366
Lots of complicated stuff there, took me a while to grasp it all