Page 1 of 1
Splitting .w !?
Posted: Sun Jul 06, 2003 3:09 pm
by Ralf
when having a value like $096C also 2412, how can i split this to have two variable handling the first and second byte? Also as result, something like:
Code: Select all
input.w = 2412
result1.w = 24
result2.w = 12
result1 and result2 needed as .w!!! Is there any way to do this with LSL, LSR (bitshifts) or any math calculations but without any string operations? I know its possible with string operations but still search a way to realise it on another way (math).
Re: Splitting .w !?
Posted: Sun Jul 06, 2003 4:13 pm
by GPI
Ralf wrote:when having a value like $096C also 2412, how can i split this to have two variable handling the first and second byte? Also as result, something like:
Code: Select all
input.w = 2412
result1.w = 24
result2.w = 12
result1 and result2 needed as .w!!! Is there any way to do this with LSL, LSR (bitshifts) or any math calculations but without any string operations? I know its possible with string operations but still search a way to realise it on another way (math).
result1=input>>8
result2=input&$FF
Re: Splitting .w !?
Posted: Sun Jul 06, 2003 4:23 pm
by Pupil
> result1=input>>8
You have to binary 'AND' this also, as PB uses Aritmethic shift, which means that if it's a negative number '1':s will be shifted in from the left...
So this would be the correct way to get the upper byte value:
Code: Select all
input.w = $ffff
result0.w = input & $ff
result1.w = input >> 8 & $ff
result2.w = input >> 8 ;& $ff
; compare results!
Debug result0
Debug result1
Debug result2
Re: Splitting .w !?
Posted: Sun Jul 06, 2003 4:39 pm
by Ralf
Having everytime 9 and 108 as result instead 24 and 12 (input.w = 2412) So input
input.w = 2412
result1=input>>8
result2=input&$FF
MessageRequester("Results:",Str(result1)+" , "+Str(result2),0)
End
Posted: Sun Jul 06, 2003 5:29 pm
by ebs
Ralf,
2412 decimal = 96c hex
Therefore:
result1 = 9 hex, 9 decimal
result2 = 6c hex, 108 decimal
Shifting by eight bits and masking the lower eight bits works for hex values, not decimal. If you want to get the decimal results, you must divide by 10^2 = 100, like this:
Code: Select all
input.w = 2412
result1.w = input/100
result2.w = input - result1*100
MessageRequester("Results:", Str(result1) +" , " + Str(result2), 0)
End
Regards,
Eric
Posted: Sun Jul 06, 2003 6:33 pm
by Ralf
Thanks all.
ebs, your code is exactly what i needed but i m not sure if there could be problems by dividing by 100. Is there no problem that i get wrong result, even of the floats by dividing by 100. I m not sure if there could be any round up/down problems!?
Posted: Sun Jul 06, 2003 8:01 pm
by ebs
Ralf,
The division by 100 should work for all decimal integer values, but not for floats.
Eric