Unsigned variable bug

Just starting out? Need help? Post your questions and find answers here.
User avatar
doctorized
Addict
Addict
Posts: 854
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: Unsigned variable bug

Post by doctorized »

Little John wrote:Who says so (except from you)?
I think the terms "signed" and "unsigned" apply when values are assigned to the respective variable, not when using it in calculations.
Look at help. .a means ascii and stores values from 0 to 255. So, by saying b.a = 2 - 4 how do you expect to store and show you a negative value? It is like you want to store 1,000,000 in a byte. You just can't.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Unsigned variable bug

Post by Little John »

doctorized wrote:
Little John wrote:Who says so (except from you)?
I think the terms "signed" and "unsigned" apply when values are assigned to the respective variable, not when using it in calculations.
Look at help. .a means ascii and stores values from 0 to 255. So, by saying b.a = 2 - 4 how do you expect to store and show you a negative value?
I did not expect that.
You might want to read again what I wrote, and try to understand it in the given context.
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Unsigned variable bug

Post by Olliv »

User_Russian wrote:In this code a-4=254. But they are calculated as signed variables. Why?

Code: Select all

a.a = 2
b.a = a-4
Debug b
Debug Bool(a-4>0)
If a-4>0
  Debug "a>0"
EndIf
Hello UserRussian,

here is my humble opinion:

"If cc" or "Bool(cc)" are on 32 or 64 range. Simply, there is no code source characters pair anymore to use CPU features. Fred took the choice to use the greatest bit as the sign status (CPU allows us to use carry flag, but Basic syntax should be overloaded, ie : a-b vs a--b, the first op with embedded sign, the second one with manual sign, it would be difficult to stay in simple syntax...)

Code: Select all

; Give -1 as it should give 1
Debug Hex((1 << 63) >> 63)
; give rightly 1
Debug Hex((1 << 62) >> 62)
You can see 63 bits are true in the first result, as we imagine the two results should have the same value...

These true bits have to be erased by a AND Mask during boolean op.

Code: Select all

Debug Bool(cc & $FF) ; for unsigned 8 bits
Debug Bool(cc & $FFFF) ; for unsigned 16 bits
Just add a type-matching mask in the boolean op :

Code: Select all

a.a = 2
b.a = a-4
Debug b
Debug Bool((a-4) & $FF>0)
If (a-4) & $FF >0
  Debug "a>0"
EndIf
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Unsigned variable bug

Post by Olliv »

Interesting subject which finds a way to the modules. Thank you.

If we change the default type, the comparison gives the respective result.

Code: Select all

DeclareModule A
Declare Test()
EndDeclareModule
DeclareModule I
Declare Test()
EndDeclareModule
Module A
Procedure Test()
Define.A
A = 2
B = A - 4
If A < B
Debug Str(A) + " < " + Str(B)
Else
Debug Str(A) + " > " + Str(B)
EndIf
EndProcedure
EndModule
Module I
Procedure Test()
Define.I
A = 2
B = A - 4
If A < B
Debug Str(A) + " < " + Str(B)
Else
Debug Str(A) + " > " + Str(B)
EndIf
EndProcedure
EndModule
A::Test()
B::Test()
Nota : Same way for procedures.

Code: Select all

Procedure Proc1()
Define.I
; ...
EndProcedure

Procedure Proc2()
Define.A
; ...
EndProcedure
Post Reply