Splitting .w !?

Just starting out? Need help? Post your questions and find answers here.
Ralf
Enthusiast
Enthusiast
Posts: 203
Joined: Fri May 30, 2003 1:29 pm
Location: Germany

Splitting .w !?

Post 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).
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Re: Splitting .w !?

Post 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
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Re: Splitting .w !?

Post 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
Ralf
Enthusiast
Enthusiast
Posts: 203
Joined: Fri May 30, 2003 1:29 pm
Location: Germany

Re: Splitting .w !?

Post 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
ebs
Enthusiast
Enthusiast
Posts: 562
Joined: Fri Apr 25, 2003 11:08 pm

Post 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
Ralf
Enthusiast
Enthusiast
Posts: 203
Joined: Fri May 30, 2003 1:29 pm
Location: Germany

Post 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!?
ebs
Enthusiast
Enthusiast
Posts: 562
Joined: Fri Apr 25, 2003 11:08 pm

Post by ebs »

Ralf,

The division by 100 should work for all decimal integer values, but not for floats.

Eric
Post Reply