# Maths making use of variable overflow

#### danners430

##### Member
Happy Friday!

I was wondering if someone on here would be able to confirm something for me:
I'm planning on using a relatively simple method for having timed functions carried out by the picaxe, while still scanning pins etc. For example, when a pin is detected to be high, 2 minutes later the picaxe sets another pin high. If the input pin is set to low, the output is immediately turned off, and the timer reset.

In my big program, I'll have multiple functions using the timer at once, so the functions store the value of the timer when the functions is originally called, then find the difference between the "realtime" timer value and the stored value.

My question is, however, if and when the timer overflows, would the calculations still work? From my reading, the maths should also simply overflow, so for example 10-245 = 20. Could someone confirm this?

Code:
``````'b0 = number of procedures using timer
'w2 = timer in multiples of 25ms

main:
if pinB.1 = 1 then
call sampleTimerFunction
else
call endTimeruse
end if

call timer

goto main

end

timer:
if b0 = 0 then		'if no functions are using timer, reset to 0 and return
w2 = 0
return
end if

pause 25			'wait for 25 ms and add 1 to timer value
w2 = w2 + 1
return

sampleTimerFunction:

'b1 = has function passed the initial stage?
'w3 = repository for timer value when function is initially called
'b2 = latch

if b1 = 0 then
w3 = w2								'store initial timer value
b0 = b0 + 1							'add this function to the "list" of functions currently using the timer
b1 = 1								'skip this stage next time function is called
end if

if w2 - w3 = 4800 or b2 = 1 then		'have 120s elapsed?
high B.0							'do something
b2 = 1								'set latch - program will now always do the above until it is reset
end if

return

endTimerUse:
b0 = b0 - 1								'remove this function from the "list" of functions currently using the timer
b2 = 0									'reset latches from sampleTimerFunction
b1 = 0
w3 = 0									'clear stored time value
low B.0									'end whatever was started when the timer elapsed
return``````

#### hippy

##### Technical Support
Staff member
10 - 245 = -235, is \$FF15, which as a positive binary integer is \$FF15 (65301) as a word or \$15 (21) as a byte.

You can confirm that by simulating the following code under PE6 -

Code:
``````b1 = 10
b2 = 245
b0 = b1 - b2
SerTxd( #b0, " = ", #b1, " - ", #b2, CR, LF )``````
Code:
``````w1 = 10
w2 = 245
w0 = w1 - w2
SerTxd( #w0, " = ", #w1, " - ", #w2, CR, LF )``````
That 21 would seem to be the correct result -

Code:
``````245 246 247 248 249 250 251 252 253 524 255
+1  +2  +3  +4  +5  +6  +7  +8  +9  +10

0   1   2   3    4   5   6   7   8   9  10
+11 +12 +13 +14 +15 +16 +17 +18 +19 +20 +21``````

#### danners430

##### Member
That should work perfectly then even though I made a mistake in my original maths... (oops)

cheers Hippy!

#### hippy

##### Technical Support
Staff member
I had expected 20 too, which is why there's that proof it is 21

245 is of course 11 below zero, 10 below 255. Things can indeed get complicated and/or confusing when passing through zero.

#### Aries

##### New Member
I've often used a spreadsheet to work out this sort of thing. MOD(x+256,256) for byte arithmetic or MOD(x+65536,65536) for word arithmetic will give you the "positive" version of negative numbers and you can check the arithmetic very easily.

#### tmfkam

##### Senior Member
I've often used a spreadsheet to work out this sort of thing. MOD(x+256,256) for byte arithmetic or MOD(x+65536,65536) for word arithmetic will give you the "positive" version of negative numbers and you can check the arithmetic very easily.
I like that! I must try to remember that one for when I'm struggling with my maths.
Thanks for sharing that one Aries.

#### danners430

##### Member
Cheers for the help guys,

Luckily I don't actually need to work explicitly with the maths, as long as it works the timer will work as intended - once a certain time difference has been reached (w1-w0 etc.), the program will carry out the procedure Saves me trying to play around with multitasking... something I like to steer clear of...

The only thing that can affect the program now are i2c commands lengthening the pause commands (slightly) - but for my use-case, it shouldn't make any difference whatsoever!!

#### newplumber

##### Senior Member
Wow I am behind times here....I get 10 - 245 = -235
but....
Code:
``````245 246 247 248 249 250 251 252 253 524 255
+1  +2  +3  +4  +5  +6  +7  +8  +9  +10

0   1   2   3    4   5   6   7   8   9  10
+11 +12 +13 +14 +15 +16 +17 +18 +19 +20 +21``````

how is it adding to 21 ....I googled alot ...even for dummies still can't figure it out
maybe its to complicated

#### hippy

##### Technical Support
Staff member
Why it's complicated is because, using two's compliment values ...

Code:
``````128 \$80 = -128
129 \$81 = -127
:
254 \$FE = -2
255 \$FF = -1
0   \$00 = +0
1   \$01 = +1
:
127 \$7F = +127``````
A byte value of 245 doesn't exist, it's really -11 ...

Code:
``````245 246 247 248 249 250 251 252 253 524 255
-11 -10 -9  -8  -7  -6  -5  -4  -3  -2  -1``````
So when we subtract 245 from 10, what we are really doing is subtracting the two's compliment value ...

10 - (-11) = 10 + 11 = 21

And, yes, it can be hard to get one's head around that!

And you are correct 10 - 245 does = -235. That 235 is \$EB, -235 is -\$EB, which is \$15 which is 21.

So everything 'adds up' to coin a phrase. It's just how one describes what the numbers represent.

Last edited:

#### newplumber

##### Senior Member
Thanks hippy ...I'm going back to the drawing board ....

Okay ... it all makes sense now ...pretty cool 0-255

B1 = 2 - 10
B1 = 246

B1 = 10 - 245
B1 = 21

B1 = 20 - 450
B1 = 82

I also learned how to minus binary with twos complement (i think anyway)
-4 + 4 =
= (1) 0100 ' -4
= (1) 1011 ' twos complement

....(1) 1011 ' -4
........... +1 ' ones complement
----------------
= (1) 1100 ' -4

........(1) 1100 '-4
....+..(0) 0100 ' 4
-------------------------
........(0) 0000

so -4 + 4 = 0

Last edited: