Help in How to detect an Overflow.
Moderators: David Barker, Jerry Messina
-
- Posts: 41
- Joined: Thu Feb 26, 2009 6:54 pm
- Location: UK
Help in How to detect an Overflow.
Hi guys,
I need some advice regarding this:
I have a counter, X, as a Byte. (It needs to be a Byte)
The counter increments and eventually it will be greater than 255.
I want to detect this overflow. How do I do this without using assembly code?
Thanks in advance.
Regards
Francesco C.
I need some advice regarding this:
I have a counter, X, as a Byte. (It needs to be a Byte)
The counter increments and eventually it will be greater than 255.
I want to detect this overflow. How do I do this without using assembly code?
Thanks in advance.
Regards
Francesco C.
replace the Inc( X ) instruction with the following:
Remember to set Overflow to false at the beginning!
Code: Select all
If X = 255 Then
Overflow = true
Else
Inc( X )
EndIf
-
- Posts: 41
- Joined: Thu Feb 26, 2009 6:54 pm
- Location: UK
Hi guys,
I need to clarify my question.
X is defined as 'Byte'. This mean it can only hold 255 max.
I x is incremented by 2.
Therefore when x=254 and the counter increments by 2, x=0.
Hence I cannot say 'If x > 255...'.
Thats why i need to check if it has excedded 255 (overflow).
Any idea?
Regards
Francesco C.
I need to clarify my question.
X is defined as 'Byte'. This mean it can only hold 255 max.
I x is incremented by 2.
Therefore when x=254 and the counter increments by 2, x=0.
Hence I cannot say 'If x > 255...'.
Thats why i need to check if it has excedded 255 (overflow).
Any idea?
Regards
Francesco C.
you can not do it with a byte, which you answered yourself.
It would need a word.
If the value is just steps of two you could simply increment a counter and multiply the answer by 2 to get X.
It would need a word.
If the value is just steps of two you could simply increment a counter and multiply the answer by 2 to get X.
Code: Select all
dim loop as byte
dim x as byte
dim overflow as boolean
loop=0
x=0
overflow=false
while overflow=false
X=loop*2
inc(loop)
if loop=128 then
overflow=true
endif
wend
-
- Posts: 41
- Joined: Thu Feb 26, 2009 6:54 pm
- Location: UK
dman776,
Thank you for the help
Yes the solution was to check the carry flag in the status register.
........
If Status.0=1 then /// the carry flag is set indicating an overflow!
do something
End If
...................
Not being very familiar with a basic compiler, I was not aware you could
check the status register so easily.
Thanks again.
Francesco C.
Thank you for the help
Yes the solution was to check the carry flag in the status register.
........
If Status.0=1 then /// the carry flag is set indicating an overflow!
do something
End If
...................
Not being very familiar with a basic compiler, I was not aware you could
check the status register so easily.
Thanks again.
Francesco C.