problem with negative long numbers

Just starting out? Need help? Post your questions and find answers here.
harkon
Enthusiast
Enthusiast
Posts: 217
Joined: Wed Nov 23, 2005 5:48 pm

problem with negative long numbers

Post by harkon »

looking at the following;

Code: Select all

;Why does a negative number return 8 bytes

LongValue.l=2147483647
Debug "As a long " + Str(LongValue) + " .. as hex " + Hex(LongValue)
Debug ""

LongValue=-2147483647
Debug "As a long " + Str(LongValue) + " .. as hex " + Hex(LongValue)
Debug "Shoud return FFFFFFFF .. right?"

Why does this now return a different Hex() string than it did in PB Version 4.2? I can allow for this in that if the number is negative I can just discard the first 4 bytes, but will this behavior remain, or is it something that needs fixing?

TIA, and thanks to the team for all of their efforts on 4.3.
Missed it by that much!!
HK
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

I think maybe because Debug casts a Quad on it?
the first Value is also Quad, but leading Zeros are not printed?

> "Shoud return FFFFFFFF .. right?"
nope. $FFFFFFFF is -1
oh... and have a nice day.
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Use #PB_Long as the second parameter to Hex() to treat it as a long value (returning FFFFFFFF in this case)
quidquid Latine dictum sit altum videtur
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

Code: Select all

LongValue = -2147483648
Debug "As a long " + Str(LongValue) + " .. as hex " + Hex(LongValue, #PB_Long)

LongValue = -1
Debug "As a long " + Str(LongValue) + " .. as hex " + Hex(LongValue, #PB_Long)

a.l = $FFFFFFFF
Debug Str(a)
oh... and have a nice day.
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Post by Little John »

freak wrote:Use #PB_Long as the second parameter to Hex() to treat it as a long value (returning FFFFFFFF in this case)
Oh, this information is another Christmas present. ;-)
In the (English and German) help for PB 4.30, it's not mentioned that Hex() can take a second parameter.

Code: Select all

Debug Hex(-1)
Debug Hex(-1, #PB_Quad)
Debug Hex(-1, #PB_Long)
Debug Hex(-1, #PB_Word)
Debug Hex(-1, #PB_Byte)
Regards, Little John
harkon
Enthusiast
Enthusiast
Posts: 217
Joined: Wed Nov 23, 2005 5:48 pm

Post by harkon »

Thanks everyone. As mentioned the help didn't say anything about this so I was clueless. #PB_Long does the trick. And, you're right it should have returned 8000001, had a brain fart there. Never was too great at base conversion :oops:

BTW I've found a few oversights in the help, like a some of the new libraries help points to an example but there doesn't seem to be an example available. I know that help authoring is a huge undertaking, so I'm very grateful for the help given.

Thanks to all for your help, got this one ported to PB 4.3 and works great. On to the next :D
Missed it by that much!!
HK
User avatar
mk-soft
Always Here
Always Here
Posts: 6207
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Post by mk-soft »

since pb v4.3 is $FFFFFFFF not equal -1 because is casts as quad

Code: Select all

Procedure.l LONG(Value.q)
  ProcedureReturn Value
EndProcedure

Procedure.b BYTE(Value.q)
  ProcedureReturn Value
EndProcedure

#c_test1 = $FFFFFFFF ; <- casts as quad
#c_test2 = -1

If #c_test1 = #c_test2
  Debug "Equal"
Else
  Debug "Error"
EndIf

If LONG(#c_test1) = #c_test2
  Debug "Equal"
Else
  Debug "Error"
EndIf

If BYTE(#c_test1) = BYTE(#c_test2)
  Debug "Equal"
Else
  Debug "Error"
EndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

debug also before had casted quads...

run this is 4.2:

Code: Select all

Debug $FFFFFFFF
and also in 4.3, a long is treated as long:

Code: Select all

a.l = $FFFFFFFF
Debug a
so, your sentence
since pb v4.3 is $FFFFFFFF not equal -1 because is casts as quad
is missing at least two parameters to be evaluated as True or False...
oh... and have a nice day.
Post Reply