Signed Binary annoying ?

Share your advanced PureBasic knowledge/code with the community.
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Signed Binary annoying ?

Post by Droopy »

Code updated For 5.20+

Convert signed binary to unsigned binary ( as long )

Code: Select all

Procedure SignedBinaryToUnsigned(Byte.b)
  If Byte & %10000000 
    Byte=Byte & %01111111
    Retour=Byte+128
  Else
    Retour=Byte
  EndIf
  ProcedureReturn Retour
EndProcedure
and for testing

Code: Select all

;/                                     Unsigned   Signed
Debug SignedBinaryToUnsigned(%00000000) ; 0         0
Debug SignedBinaryToUnsigned(%01111111) ; 127       127
Debug SignedBinaryToUnsigned(%10000000) ; 128       -128
Debug SignedBinaryToUnsigned(%11111111) ; 255       -1
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Re: Signed Binary annoying ?

Post by Pupil »

Droopy wrote:Convert signed binary to unsigned binary ( as long )

Code: Select all

Procedure SignedBinaryToUnsigned(Byte.b)
  If Byte & %10000000 
    Byte=Byte & %01111111
    Retour=Byte+128
  Else
    Retour=Byte
  EndIf
  ProcedureReturn Retour
EndProcedure
and for testing

Code: Select all

;/                                     Unsigned   Signed
Debug SignedBinaryToUnsigned(%00000000) ; 0         0
Debug SignedBinaryToUnsigned(%01111111) ; 127       127
Debug SignedBinaryToUnsigned(%10000000) ; 128       -128
Debug SignedBinaryToUnsigned(%11111111) ; 255       -1
Why not use this?...

Code: Select all

Procedure SignedBinaryToUnsigned(byte.b)
  ProcedureReturn byte & $ff
EndProcedure
... or just using this 'MyByteVar & $ff' inline instead!
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

Thanks I found this Fred code in the Us Forum this morning

Thanks :D
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post by traumatic »

Droopy wrote:...in the Us Forum...
Where?
Good programmers don't comment their code. It was hard to write, should be hard to read.
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post by traumatic »

Thanks Droopy, I just wondered what the US-forums were ;)
Good programmers don't comment their code. It was hard to write, should be hard to read.
Post Reply