Does any one here know how to perform Two's Compliment on a binary number in PB?
Thanks,
Killswitch
Two's Compliment
-
Killswitch
- Enthusiast

- Posts: 731
- Joined: Wed Apr 21, 2004 7:12 pm
Two's Compliment
Last edited by Killswitch on Wed Oct 19, 2005 9:39 pm, edited 1 time in total.
~I see one problem with your reasoning: the fact is thats not a chicken~
-
Killswitch
- Enthusiast

- Posts: 731
- Joined: Wed Apr 21, 2004 7:12 pm
-
Killswitch
- Enthusiast

- Posts: 731
- Joined: Wed Apr 21, 2004 7:12 pm
Try this : ( Author : fweil )
Code: Select all
ProcedureDLL.l Bin2Dec(BinaryStringNumber.s)
*t.OneByte = @BinaryStringNumber
Result.l = 0
While *t\a <> 0
Result = (Result << 1) + (*t\a - 48)
*t + 1
Wend
ProcedureReturn Result
EndProcedure Had a little trouble with this procedure so i've written another one based upon it, it could do with some validation on the binary string though!Droopy wrote:Try this : ( Author : fweil )
Code: Select all
Procedure.l ValB(String.s)
Protected Result.l
Protected Pointer.l
Pointer.l = @String
For x = 1 To Len(String)
Result = (Result << 1) + (PeekB(Pointer) - 48)
Pointer + 1
Next x
ProcedureReturn Result
EndProcedure
TestBinary1.b = %01101011
TestBinary2.w = %0110101100111111
TestBinary3.l = %01101011011111110110111100110011
Debug "Binary Numbers:"
Debug TestBinary1
Debug TestBinary2
Debug TestBinary3
Debug ""
TestString1.s = "01101011"
TestString2.s = "0110101100111111"
TestString3.s = "01101011011111110110111100110011"
Debug "Binary Numbers From Strings:"
Debug ValB(TestString1)
Debug ValB(TestString2)
Debug ValB(TestString3)

