View previous topic :: View next topic |
Author |
Message |
SnarlingSheep Professional Member
Joined: 13 Mar 2001 Posts: 759 Location: Michigan
|
Posted: Fri Aug 01, 2003 9:48 pm Post subject: Bitwise AND, OR, XOR, NOT, Bit Shifting and RGB. |
|
|
NOTE: Functions only work with integers
Bitwise AND, OR, XOR, NOT, Shift-Left and Shift-Right.
Also has a RGB function for returning a value equivalent to an API COLORREF.
10 functions:
-SSBitAnd: Bitwise AND on 2 numbers, same as (25 & 5).
-SSBitOR: Bitwise OR on 2 numbers, same as (25 | 5).
-SSBitXOR: Bitwise XOR on 2 numbers, same as (25 ^ 5).
-SSBitNOT: Bitwise NOT on a number.
-SSBitRShift: Bitwise right-shift of n1 by n2 places, same as (25 >> 5)
-SSBitLShift: Bitwise left-shift of n1 by n2 places, same as (25 << 5)
-SSRGB: Returns a COLORREF value based on 3 inputed numbers, same as RGB(0,0,255) which returns a value for Blue.
-SSHex2Dec: only needed if you want to perform an AND, OR or XOR on a hex number.
-SSDec2Bin: Used by the main functions to convert Decimal to it's Binary equivelant.
-SSBin2Dec: Used by the main functions to convert Binary back to Decimal.
::Thanks to Mindpower for the NOT and Shifting code, although I changed the shifts to return the same results as C's << and >>.
Code: |
#DEFINE FUNCTION,SSBitAND
#DEFINE FUNCTION,SSBitXOR
#DEFINE FUNCTION,SSBitOR
#DEFINE FUNCTION,SSBitNOT
#DEFINE FUNCTION,SSHex2Dec
#DEFINE FUNCTION,SSDec2Bin
#DEFINE FUNCTION,SSBin2Dec
#DEFINE FUNCTION,SSBitRShift
#DEFINE FUNCTION,SSBitLShift
#DEFINE FUNCTION,SSRGB
REM 15 & 2 = 2
REM INFO @SSBitAND(15,2)
REM 15 | 2 = 15
REM INFO @SSBitOR(15,2)
REM 15 ^ 2 = 13
REM INFO @SSBitXOR(15,2)
REM NOT 25 = -26
REM INFO @SSBitNOT(25)
REM 15 >> 2 = 3
REM INFO @SSBitRShift(15,2)
REM 15 << 2 = 60
REM INFO @SSBitLShift(15,2)
REM RGB(255,0,255) = 16711935 (LIME - COLORREF value you can pass to various API's)
INFO @SSRGB(255,0,255)
exit
:SSBitAnd
%%SSBAresult = ""
%%SSBA1 = @SSDec2Bin(%1)
%%SSBA2 = @SSDec2Bin(%2)
IF @GREATER(@LEN(%%SSBA1),@LEN(%%SSBA2))
%%SSBAStop = @LEN(%%SSBA1)
%%SSBACheck = 1
ELSE
%%SSBAStop = @LEN(%%SSBA2)
%%SSBACheck = 2
END
WHILE @NOT(@EQUAL(@LEN(%%SSBA2),@LEN(%%SSBA1)))
IF @EQUAL(%%SSBACheck,1)
%%SSBA2 = "0"%%SSBA2
ELSE
%%SSBA1 = "0"%%SSBA1
END
WEND
%%SSBAX = 1
REPEAT
IF @BOTH(@EQUAL(@SUBSTR(%%SSBA1,%%SSBAX,%%SSBAX),1),@EQUAL(@SUBSTR(%%SSBA2,%%SSBAX,%%SSBAX),1))
%%SSBAResult = %%SSBAResult"1"
ELSE
%%SSBAResult = %%SSBAResult"0"
END
%%SSBAX = @SUCC(%%SSBAX)
UNTIL @GREATER(%%SSBAX,%%SSBAStop)
%%SSBAresult = @SSBin2Dec(%%SSBAresult)
exit %%SSBAresult
:SSBitOR
%%SSBOresult = ""
%%SSBO1 = @SSDec2Bin(%1)
%%SSBO2 = @SSDec2Bin(%2)
IF @GREATER(@LEN(%%SSBO1),@LEN(%%SSBO2))
%%SSBOStop = @LEN(%%SSBO1)
%%SSBOCheck = 1
ELSE
%%SSBOStop = @LEN(%%SSBO2)
%%SSBOCheck = 2
END
WHILE @NOT(@EQUAL(@LEN(%%SSBO2),@LEN(%%SSBO1)))
IF @EQUAL(%%SSBOCheck,1)
%%SSBO2 = "0"%%SSBO2
ELSE
%%SSBO1 = "0"%%SSBO1
END
WEND
%%SSBOX = 1
REPEAT
IF @EQUAL(@SUBSTR(%%SSBO1,%%SSBOX,%%SSBOX),1)@EQUAL(@SUBSTR(%%SSBO2,%%SSBOX,%%SSBOX),1)
%%SSBOResult = %%SSBOResult"1"
ELSE
%%SSBOResult = %%SSBOResult"0"
END
%%SSBOX = @SUCC(%%SSBOX)
UNTIL @GREATER(%%SSBOX,%%SSBOStop)
%%SSBOresult = @SSBin2Dec(%%SSBOresult)
exit %%SSBOresult
:SSBitXOR
%%SSBXOresult = ""
%%SSBXO1 = @SSDec2Bin(%1)
%%SSBXO2 = @SSDec2Bin(%2)
IF @GREATER(@LEN(%%SSBXO1),@LEN(%%SSBXO2))
%%SSBXOStop = @LEN(%%SSBXO1)
%%SSBXOCheck = 1
ELSE
%%SSBXOStop = @LEN(%%SSBXO2)
%%SSBXOCheck = 2
END
WHILE @NOT(@EQUAL(@LEN(%%SSBXO2),@LEN(%%SSBXO1)))
IF @EQUAL(%%SSBXOCheck,1)
%%SSBXO2 = "0"%%SSBXO2
ELSE
%%SSBXO1 = "0"%%SSBXO1
END
WEND
%%SSBXOX = 1
REPEAT
IF @BOTH(@EQUAL(@SUBSTR(%%SSBXO1,%%SSBXOX,%%SSBXOX),1),@EQUAL(@SUBSTR(%%SSBXO2,%%SSBXOX,%%SSBXOX),1))
%%SSBXOResult = %%SSBXOResult"0"
ELSIF @BOTH(@ZERO(@SUBSTR(%%SSBXO1,%%SSBXOX,%%SSBXOX)),@ZERO(@SUBSTR(%%SSBXO2,%%SSBXOX,%%SSBXOX)))
%%SSBXOResult = %%SSBXOResult"0"
ELSE
%%SSBXOResult = %%SSBXOResult"1"
END
%%SSBXOX = @SUCC(%%SSBXOX)
UNTIL @GREATER(%%SSBXOX,%%SSBXOStop)
%%SSBXOresult = @SSBin2Dec(%%SSBXOresult)
exit %%SSBXOresult
:SSDec2Bin
%%SSD2Bresult = ""
%%SSD2BintValue = %1
While @GREATER(%%SSD2BintValue,0)
%%SSD2Bi = @MOD(%%SSD2BIntValue,2)
%%SSD2Bi = @FADD(%%SSD2Bi,1)
%%SSD2BintValue = @DIV(%%SSD2BintValue,2)
%%SSD2Bresult = @SUBSTR("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",%%SSD2Bi,%%SSD2Bi)%%SSD2Bresult
WEND
exit %%SSD2Bresult
:SSBin2Dec
%%SSB2DResult = 0
%%SSB2DX = 1
%%SSB2DStrValue = %1
REPEAT
%%SSB2DCharValue = @POS(@UPPER(@SUBSTR(%%SSB2DStrValue,%%SSB2DX,%%SSB2DX)),"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
#return -1 if character is not supported by base
If @GREATER(%%SSB2DCharValue,2)
%%SSB2DResult = -1
%%SSB2DX = @SUCC(@LEN(%%SSB2DStrValue))
END
%%SSB2DResult = @FADD(@FMUL(%%SSB2DResult,2),@FSUB(%%SSB2DcharValue,1))
%%SSB2DX = @SUCC(%%SSB2DX)
UNTIL @GREATER(%%SSB2DX,@LEN(%%SSB2DStrValue))
exit %%SSB2DResult
:SSHex2Dec
%%SSH2DHex = %1
%%SSH2D = 0
%%SSH2DX = 1
repeat
%%SSH2DChar = @UPPER(@SUBSTR(%%SSH2DHex,%%SSH2DX,%%SSH2DX))
%%SSH2DPos = @FSUB(@POS(%%SSH2DChar,"0123456789ABCDEF"),1)
%%SSH2D = @FADD(@FMUL(%%SSH2D,16),%%SSH2DPos)
%%SSH2DX = @SUCC(%%SSH2DX)
until @GREATER(%%SSH2DX,@LEN(%%SSH2DHex))
exit %%SSH2D
:SSBitNOT
if @Equal(@Pos(-,%1),1)
%%SSBitNot = @fsub(@StrDel(%1,1),1)
Else
%%SSBitNot = @fsub(-%1,1)
End
Exit %%SSBitNot
:SSBitRShift
%%SSBRSresult = " "
%%SSBRS1 = ""
%%SSBRS2 = @SSDec2Bin(%1)
%%SSBRSx = 0
repeat
%%SSBRS1 = %%SSBRS1"0"
%%SSBRSx = @SUCC(%%SSBRSx)
until @EQUAL(%%SSBRSx,%2)
%%SSBRS1 = %%SSBRS1%%SSBRS2
%%SSBRSresult = @SSBin2Dec(@SUBSTR(%%SSBRS1,1,@LEN(%%SSBRS2)))
exit %%SSBRSresult
:SSBitLShift
%%SSBLSresult = " "
%%SSBLS1 = @SSDec2Bin(%1)
%%SSBLS2 = @SSDec2Bin(%1)
%%SSBLSx = 0
repeat
%%SSBLS1 = %%SSBLS1"0"
%%SSBLSx = @SUCC(%%SSBLSx)
until @EQUAL(%%SSBLSx,%2)
REM if (@GREATER(@LEN(%%SSBLS1),8)
REM %%SSBLScut = @FSUB(@LEN(%%SSBLS1),8)
REM %%SSBLSresult = @SSBin2Dec(@SUBSTR(%%SSBLS1,%%SSBLScut,@SUM(%%SSBLScut,8)))
REM else
%%SSBLSresult = @SSBin2Dec(%%SSBLS1)
REM end
exit %%SSBLSresult
:SSRGB
%%SSRed = %1
%%SSGreen = %2
%%SSBlue = %3
%%SSGreen = @SSBitLShift(%%SSGreen,8)
%%SSBlue = @SSBitLShift(%%SSBlue,16)
%%SSRGBResult = @SSBitOR(%%SSRed,%%SSGreen)
%%SSRGBResult = @SSBitOR(%%SSRGBResult,%%SSBLue)
exit %%SSRGBResult
|
_________________ -Sheep
My pockets hurt...
Last edited by SnarlingSheep on Fri Nov 27, 2009 3:01 pm; edited 4 times in total |
|
Back to top |
|
|
FreezingFire Admin Team
Joined: 23 Jun 2002 Posts: 3508
|
Posted: Fri Aug 01, 2003 9:58 pm Post subject: |
|
|
Nice work. _________________ FreezingFire
VDSWORLD.com
Site Admin Team |
|
Back to top |
|
|
SnarlingSheep Professional Member
Joined: 13 Mar 2001 Posts: 759 Location: Michigan
|
Posted: Fri Aug 01, 2003 10:42 pm Post subject: |
|
|
UPDATED: Now has AND, OR and XOR functions. _________________ -Sheep
My pockets hurt... |
|
Back to top |
|
|
CodeScript Moderator Team
Joined: 08 Jun 2003 Posts: 1060 Location: India
|
Posted: Sat Aug 02, 2003 4:50 am Post subject: |
|
|
Nice work. Good when one doesn't have to process a large amount of data(encrypting a file) or when speed is everything . Sincirely I haven't benchmarked them for the speed differerce though - it may be small ?.
Also i saw several codes in VB for converting a 32num to loword etc they had a limitation that beyond a particular number there is need for a different formula. SnarlingSheep is there any such limitation here above ? ( i have always been bad at math) _________________ Regards
- CodeScript
Give your application a professional look with the VDSGUI Extension |
|
Back to top |
|
|
SnarlingSheep Professional Member
Joined: 13 Mar 2001 Posts: 759 Location: Michigan
|
Posted: Sat Aug 02, 2003 5:33 am Post subject: |
|
|
I'm no math wiz either..so if anyone finds any limitations let me know _________________ -Sheep
My pockets hurt... |
|
Back to top |
|
|
vdsalchemist Admin Team
Joined: 23 Oct 2001 Posts: 1448 Location: Florida, USA
|
Posted: Fri Aug 15, 2003 8:34 pm Post subject: |
|
|
Hi SnarlingSheep,
I must say this is pretty impressive but I have only one correction to make... 0 xor 0 is not equal to 1. 0 xor 0 is equal to 0. Just like 1 xor 1 is equal to 0. Below is a corrected version of your Xor function...
Code: | :SSBitXOR
%%SSBXOresult = ""
%%SSBXO1 = @SSDec2Bin(%1)
%%SSBXO2 = @SSDec2Bin(%2)
IF @GREATER(@LEN(%%SSBXO1),@LEN(%%SSBXO2))
%%SSBXOStop = @LEN(%%SSBXO1)
%%SSBXOCheck = 1
ELSE
%%SSBXOStop = @LEN(%%SSBXO2)
%%SSBXOCheck = 2
END
WHILE @NOT(@EQUAL(@LEN(%%SSBXO2),@LEN(%%SSBXO1)))
IF @EQUAL(%%SSBXOCheck,1)
%%SSBXO2 = "0"%%SSBXO2
ELSE
%%SSBXO1 = "0"%%SSBXO1
END
WEND
%%SSBXOX = 1
REPEAT
IF @BOTH(@EQUAL(@SUBSTR(%%SSBXO1,%%SSBXOX,%%SSBXOX),1),@EQUAL(@SUBSTR(%%SSBXO2,%%SSBXOX,%%SSBXOX),1))
%%SSBXOResult = %%SSBXOResult"0"
ELSIF @BOTH(@ZERO(@SUBSTR(%%SSBXO1,%%SSBXOX,%%SSBXOX)),@ZERO(@SUBSTR(%%SSBXO2,%%SSBXOX,%%SSBXOX)))
%%SSBXOResult = %%SSBXOResult"0"
ELSE
%%SSBXOResult = %%SSBXOResult"1"
END
%%SSBXOX = @SUCC(%%SSBXOX)
UNTIL @GREATER(%%SSBXOX,%%SSBXOStop)
%%SSBXOresult = @SSBin2Dec(%%SSBXOresult)
exit %%SSBXOresult
|
Have fun bitwizards _________________ Home of
Give VDS a new purpose!
|
|
Back to top |
|
|
vdsalchemist Admin Team
Joined: 23 Oct 2001 Posts: 1448 Location: Florida, USA
|
Posted: Fri Aug 15, 2003 9:33 pm Post subject: |
|
|
Hi SnarlingSheep,
Here is a couple more bitwise operators to add to your Unit You will have to add these in with the rest because they use your SSBin2Dec and SSDec2Bin functions above...
Code: | #DEFINE FUNCTION,SSBitRShift
#DEFINE FUNCTION,SSBitLShift
%%RShift = @SSBitRShift(@asc(a),2)
Info @asc(a) << 2 = %%RShift
Info %%RShift >> 2 = @SSBitLShift(%%RShift,2)
Stop
:SSBitRShift
%%SSBRSresult = ""
%%SSBRS1 = @SSDec2Bin(%1)
%U = @len(%%SSBRS1)
%%SSBRS2 = %2
%T = 0
%S =
repeat
%%SSBRS1 = "0"%%SSBRS1
%T = @Succ(%T)
Until @Greater(%T,%%SSBRS2)
if @Greater(@len(%%SSBRS1),8)
%%SSBRSresult = @SSBin2Dec(@strdel(%%SSBRS1,@succ(%U),@Sum(%U,%%SSBRS2)))
Else
%%SSBRSresult = @SSBin2Dec(%%SSBRS1)
End
exit %%SSBRSresult
:SSBitLShift
%%SSBLSresult = ""
%%SSBLS1 = @SSDec2Bin(%1)
%%SSBLS2 = %2
%T = 0
repeat
%%SSBLS1 = %%SSBLS1"0"
%T = @Succ(%T)
Until @Equal(%T,%2)
if @Greater(@len(%%SSBLS1),8)
%%SSBLSresult = @SSBin2Dec(@strdel(%%SSBLS1,1,%%SSBLS2))
Else
%%SSBLSresult = @SSBin2Dec(%%SSBLS1)
End
exit %%SSBLSresult
|
Anyway now you can have even more fun with bitwise rotation... These functions rotate a single byte (first Argument) by the number of bits in the (Second Argument) either left or right... _________________ Home of
Give VDS a new purpose!
Last edited by vdsalchemist on Tue Aug 26, 2003 9:26 pm; edited 1 time in total |
|
Back to top |
|
|
vdsalchemist Admin Team
Joined: 23 Oct 2001 Posts: 1448 Location: Florida, USA
|
Posted: Tue Aug 26, 2003 9:25 pm Post subject: |
|
|
Hi SnarlingSheep,
Here is another Bitwise function for ya. And I think that is all of them that you would need.
This just does a bitwise Not. It's very simple but extremly powerful if you mix this with the AND, OR, XOR....
Code: | #DEFINE FUNCTION,SSBitNot
%A = @SSBitNot(25)
Info %A@CR()
%B = @SSBitNot(%A)
Info %B@CR()
Stop
:SSBitNot
if @Equal(@Pos(-,%1),1)
%%SSBitNot = @fsub(@StrDel(%1,1),1)
Else
%%SSBitNot = @fsub(-%1,1)
End
Exit %%SSBitNot |
EDITED: Now this function can handle negative numbers as well so you can check your work
Edit2: I forgot to note...I am using floating point math here. So that means that the answer will not be the same as the Windows calculator if you pass a floating point number to this function. Normally the Bitwise NOT operation only works with whole numbers. I am only using the fsub so you don't get math errors with really large/small numbers. _________________ Home of
Give VDS a new purpose!
|
|
Back to top |
|
|
SnarlingSheep Professional Member
Joined: 13 Mar 2001 Posts: 759 Location: Michigan
|
Posted: Thu Oct 21, 2004 7:19 pm Post subject: |
|
|
Updated, now has:
Bitwise AND, OR, XOR, NOT, Shift-Left, Shift-Right and a RGB function. _________________ -Sheep
My pockets hurt... |
|
Back to top |
|
|
vdsalchemist Admin Team
Joined: 23 Oct 2001 Posts: 1448 Location: Florida, USA
|
Posted: Mon Mar 27, 2006 6:36 pm Post subject: |
|
|
Hey SS,
Below is a few more Bitwise things that just came up.
Code: |
#DEFINE FUNCTION,SSHIWORD
#DEFINE FUNCTION,SSLOWORD
#DEFINE FUNCTION,SSHIBYTE
#DEFINE FUNCTION,SSLOBYTE
#DEFINE FUNCTION,SSMAKEWORD
#DEFINE FUNCTION,SSMAKELONG
:Example
%%wParam = 123123123
%%Loword = @SSLOWORD(%%wParam)
%%Hiword = @SSHIWORD(%%wParam)
%%Lobyte = @SSLOBYTE(%%Loword)
%%Hibyte = @SSHIBYTE(%%Loword)
%%mkWord = @SSMAKEWORD(%%Lobyte,%%Hibyte)
%%mklong = @SSMAKELONG(%%mkWord,%%Hiword)
Info Loword = %%Loword@CR()Hiword = %%Hiword@CR()Lobyte = %%Lobyte@CR()Hibyte = %%Hibyte@CR()MakeWord = %%mkWord@CR()MakeLong = %%mklong@CR()Original = %%wParam
Exit
:SSHIWORD
# To get the HIWord of a DWORD number
%a = %1
%%SSBRS16 = @SSBitRShift(%a,16)
%%SSHWResult = @SSBitAnd(%%SSBRS16,65535)
Exit %%SSHWResult
:SSLOWORD
# To get the LOWord of a DWORD number
%a = %1
%%SSLWResult = @SSBitAnd(%a,65535)
Exit %%SSLWResult
:SSHIBYTE
#To get the HIByte of a WORD number
%a = %1
%%SSBRS = @SSBitRShift(%a,
%%SSHBResult = @SSBitAnd(%%SSBRS,255)
Exit %%SSHBResult
:SSLOBYTE
#To get the LOByte of a WORD number
%a = %1
%%SSLBResult = @SSBitAnd(%a,255)
Exit %%SSLBResult
:SSMAKEWORD
#To make a WORD from 2 BYTE values
%a = %1
%b = %2
%%SSBLS = @SSBitLShift(%b,
%%SSMWResult = @SSBitOr(%a,%%SSBLS)
Exit %%SSMWResult
:SSMAKELONG
#To make a DWORD from 2 WORD values
%a = %1
%b = %2
%%SSBLS = @SSBitLShift(%b,16)
%%SSMLResult = @SSBitOr(%a,%%SSBLS)
Exit %%SSMLResult
|
Ok I have updated this and made sure they work with an included example for ya. You will need the rest of the SSBitwise functions for these to work. _________________ Home of
Give VDS a new purpose!
|
|
Back to top |
|
|
vdsalchemist Admin Team
Joined: 23 Oct 2001 Posts: 1448 Location: Florida, USA
|
Posted: Thu Jul 10, 2008 6:20 pm Post subject: |
|
|
Hey SS,
Here is more code... I made a centralized function called @baseconv() to convert a number from base 2 to 62. @baseconv() requires @vdsfpow() to perform the conversions. Please see the code below...
Code: |
#:SSDec2Bin
# %%SSD2Bresult = ""
# %%SSD2BintValue = %1
# While @GREATER(%%SSD2BintValue,0)
# %%SSD2Bi = @MOD(%%SSD2BIntValue,2)
# %%SSD2Bi = @FADD(%%SSD2Bi,1)
# %%SSD2BintValue = @DIV(%%SSD2BintValue,2)
# %%SSD2Bresult = @SUBSTR("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",%%SSD2Bi,%%SSD2Bi)%%SSD2Bresult
# WEND
# exit %%SSD2Bresult
:SSDec2Bin
# Replaced with a central function to perform generic base conversions.
%%SSD2BintValue = %1
%%SSD2Bresult = @BaseConv(%%SSD2BintValue,10,2)
Exit %%SSD2Bresult
#:SSBin2Dec
# %%SSB2DResult = 0
# %%SSB2DX = 1
# %%SSB2DStrValue = %1
# REPEAT
# %%SSB2DCharValue = @POS(@UPPER(@SUBSTR(%%SSB2DStrValue,%%SSB2DX,%%SSB2DX)),"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
#return -1 if character is not supported by base
# If @GREATER(%%SSB2DCharValue,2)
# %%SSB2DResult = -1
# %%SSB2DX = @SUCC(@LEN(%%SSB2DStrValue))
# END
# %%SSB2DResult = @FADD(@FMUL(%%SSB2DResult,2),@FSUB(%%SSB2DcharValue,1))
# %%SSB2DX = @SUCC(%%SSB2DX)
# UNTIL @GREATER(%%SSB2DX,@LEN(%%SSB2DStrValue))
# exit %%SSB2DResult
:SSBin2Dec
# Replaced with a central function to perform generic base conversions.
%%SSB2DStrValue = %1
%%SSB2Dresult = @BaseConv(%%SSB2DStrValue,2,10)
Exit %%SSB2Dresult
#:SSHex2Dec
# %%SSH2DHex = %1
# %%SSH2D = 0
# %%SSH2DX = 1
# repeat
# %%SSH2DChar = @UPPER(@SUBSTR(%%SSH2DHex,%%SSH2DX,%%SSH2DX))
# %%SSH2DPos = @FSUB(@POS(%%SSH2DChar,"0123456789ABCDEF"),1)
# %%SSH2D = @FADD(@FMUL(%%SSH2D,16),%%SSH2DPos)
# %%SSH2DX = @SUCC(%%SSH2DX)
# until @GREATER(%%SSH2DX,@LEN(%%SSH2DHex))
# exit %%SSH2D
:SSHex2Dec
# Replaced with a central function to perform generic base conversions.
%%SSH2DHex = %1
%%SSH2D = @BaseConv(%%SSH2DHex,16,10)
Exit %%SSH2D
:BaseConv
# This function will convert any number base to any number base from base2 to base62
# Returns the %%InNumber variable as %R converted to the requested base in %%OutBase.
%R =
# Value to convert
%%InNumber = %1
# Base for %%InNumber
%%InBase = %2
# Base to convert %%InNumber too
%%OutBase = %3
# Data dictionary
%D = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
%%MaxBase = @len(%D)
%%DecVal = 0
If @Greater(%%InBase,%%MaxBase)@Greater(%%OutBase,%%MaxBase)@Less(%%InBase,2)@Less(%%OutBase,2)
# If the bases specified are above 62 or below 2 throw Arithmetic error
Error 26
Else
# Start by converting the value to Base10
%%InNumberLen = @Len(%%InNumber)
%%DecVal = 0
%J = 1
Repeat
%K = 1
Repeat
# Hmmm I wouldn't try to figure this loop out just use the converter....:)
# Basicly it treats the %D data dictionary and the input value like the old decoder rings.
# I guess I am showing my age with this one lol...
If @Equal(@substr(%%InNumber,%J,%J),@SubStr(%D,%K,%K),EXACT)
%%DecVal = @Sum(%%DecVal,@Name(@fadd(@fmul(@pred(%K),@VDSFPow(%%InBase,@diff(%%InNumberLen,%J))),.5)))
end
%K = @succ(%K)
Until @Greater(%K,%%InBase)
%J = @succ(%J)
Until @Greater(%J,%%InNumberLen)
# Now convert the value to the requested Base
Repeat
%X = @Mod(%%DecVal,%%OutBase)
If @Greater(%X,9)
%X = @succ(%X)
%R = @substr(%D,%X,%X)%R
Else
%R = %X%R
End
%%DecVal = @div(%%DecVal,%%OutBase)
Until @Zero(%%DecVal)
End
Exit %R
:VDSFPOW
# A simple function to raise a number
# to the power of some number using the VDS
# builtin floating point arithmitic functions
#x = a^b
%a = %1
%b = %2
%%aisneg = @pos(-,%a)
%%bisneg = @pos(-,%b)
If @equal(%%aisneg,1)
%a = @strdel(%a,1)
End
%x = @fmul(%b,@fln(%a))
if @equal(%%aisneg,1)
# really needs a floating mod here but if your concerned
# about not having a floating point mod then you can use
# the GadgetX.dll @lfmod function.
%%iseven = @mod(@fint(%b),2)
if @Zero(%%iseven)
%x = @fexp(%x)
else
%x = -@fexp(%x)
end
else
%x = @fexp(%x)
end
Exit %x
|
_________________ Home of
Give VDS a new purpose!
|
|
Back to top |
|
|
uvedese Contributor
Joined: 21 Jan 2006 Posts: 169 Location: Spain
|
Posted: Sun Jul 13, 2008 2:02 pm Post subject: |
|
|
Hi everybody
CodeScript wrote
Quote: | Nice work. Good when one doesn't have to process a large amount of data(encrypting a file) or when speed is everything |
There is a way to gain speed in the Dec2Bin function. We can divide by 16, not by 2 and a list has the values of operations...
Code: | #define function,Dec2Bin
list create,1
list loadtext,1
"0000
"0001
"0010
"0011
"0100
"0101
"0110
"0111
"1000
"1001
"1010
"1011
"1100
"1101
"1110
"1111
info @dec2bin(255)
stop
:Dec2Bin
%%D2Bresult = ""
while @greater(%1,0)
%%D2Bresult = @item(1,@mod(%1,16))%%D2Bresult
%1 = @div(%1,16)
wend
exit @substr(%%D2Bresult"X",@pos(1,%%D2Bresult),-1) |
Good job SnarlingSheep and dragonsphere
____________
uVeDeSe
____________ |
|
Back to top |
|
|
vdsalchemist Admin Team
Joined: 23 Oct 2001 Posts: 1448 Location: Florida, USA
|
Posted: Mon Jul 14, 2008 2:46 pm Post subject: |
|
|
uvedese,
If you try my new @BaseConv() you will find that it is pretty fast. I am not saying it is as fast as a non-interpreted language but it should be fast enough to do want you want. As for CodeScript's comment I think he was comparing to a non-interpreted language such as Delphi, C/C++, and others. Besides all these functions are in GadgetX and I think CodeScript has a DLL that does the same so if you need speed IMHO that is the way I would go. _________________ Home of
Give VDS a new purpose!
|
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|