28,406
社区成员
发帖
与我相关
我的任务
分享
Dim dblDAT
dblDAT= 185999660 and 4294965248
response.write dblDAT '本应显示185999360
'大整数逻辑与(AND)运算
Public Function And2(ByVal A, ByVal B)
'and2=(A\65536 and B\65536) *65536 + ( (A mod 65536) and (B mod 65536) ) '使用\ 或 mod 就溢出
and2=( INT(A/65536) and INT(B/65536) ) * 65536 + ( (A-int(A/65536) * 65536) and (B-int(B/65536) * 65536) )
End Function
Response.Write And2(185999660, 4294965248) &"(结果是185999360)<br>"
Response.Write And2(99775533113355,123456789) &"(结果是85016577)<br>"
'--------------------------------------------------------------------
' LShift 整数value左移bits位
'--------------------------------------------------------------------
Public Function LShift(ByVal value, ByVal bits)
LShift = Unsigned(value * (2 ^ bits))
End Function
'--------------------------------------------------------------------
' RShift 整数value右移bits位
'--------------------------------------------------------------------
Public Function RShift(ByVal value, ByVal bits)
RShift = Unsigned(Int(value / (2 ^ bits)))
End Function
'大整数n1和n2取模
Public Function Mod2(ByVal n1, ByVal n2)
If n1 < n2 Then
Mod2 = n1
ElseIf n1 = n2 Then
Mod2 = 0
Else
Mod2 = n1 - Int(n1 / n2) * n2
End If
End Function
'将一个未知符号的整数转为正整数
Public Function Unsigned(ByVal n)
If n < 0 Then
Unsigned = n + 4294967296
Else
Unsigned = Mod2(n, 4294967296)
End If
End Function
'Long整数转为四个元素的字节数组
Public Function N2W(ByVal n)
Dim r(3)
r(0) = RShift(n, 24) And &HFF
r(1) = RShift(n, 16) And &HFF
r(2) = RShift(n, 8) And &HFF
r(3) = RShift(LShift(n, 24), 24) And &HFF
N2W = r
End Function
'四个元素的字节数组转为Long整数
Public Function W2N(w)
W2N = Unsigned(LShift(w(0), 24) + LShift(w(1), 16) + LShift(w(2), 8) + w(3))
End Function
'两个字节数组相加并返回新数组
Public Function AddW(w1, w2)
Dim r(3), x
x = w1(3) + w2(3)
r(3) = x Mod 256
x = w1(2) + w2(2) + (x \ 256)
r(2) = x Mod 256
x = w1(1) + w2(1) + (x \ 256)
r(1) = x Mod 256
x = w1(0) + w2(0) + (x \ 256)
r(0) = x Mod 256
AddW = r
End Function
'两个字节数组逻辑或运算并返回新数组
Public Function OrW(w1, w2)
Dim r(3)
r(0) = (w1(0) Or w2(0)) And &HFF
r(1) = (w1(1) Or w2(1)) And &HFF
r(2) = (w1(2) Or w2(2)) And &HFF
r(3) = (w1(3) Or w2(3)) And &HFF
OrW = r
End Function
'两个字节数组逻辑与运算并返回新数组
Public Function AndW(w1, w2)
Dim r(3)
r(0) = (w1(0) And w2(0)) And &HFF
r(1) = (w1(1) And w2(1)) And &HFF
r(2) = (w1(2) And w2(2)) And &HFF
r(3) = (w1(3) And w2(3)) And &HFF
AndW = r
End Function
'两个字节数组逻辑非运算并返回新数组
Public Function NotW(w)
Dim r(3)
r(0) = (Not w(0)) And &HFF
r(1) = (Not w(1)) And &HFF
r(2) = (Not w(2)) And &HFF
r(3) = (Not w(3)) And &HFF
NotW = r
End Function
'两个字节数组逻辑异或运算并返回新数组
Public Function XorW(w1, w2)
Dim r(3)
r(0) = (w1(0) Xor w2(0)) And &HFF
r(1) = (w1(1) Xor w2(1)) And &HFF
r(2) = (w1(2) Xor w2(2)) And &HFF
r(3) = (w1(3) Xor w2(3)) And &HFF
XorW = r
End Function
'两个整数逻辑或运算并返回结果
Public Function Or2(ByVal n1, ByVal n2)
Dim w1, w2
w1 = N2W(n1)
w2 = N2W(n2)
w1(0) = (w1(0) Or w2(0)) And &HFF
w1(1) = (w1(1) Or w2(1)) And &HFF
w1(2) = (w1(2) Or w2(2)) And &HFF
w1(3) = (w1(3) Or w2(3)) And &HFF
Or2 = W2N(w1)
End Function
'两个整数逻辑与运算并返回结果
Public Function And2(ByVal n1, ByVal n2)
Dim w1, w2
w1 = N2W(n1)
w2 = N2W(n2)
w1(0) = (w1(0) And w2(0)) And &HFF
w1(1) = (w1(1) And w2(1)) And &HFF
w1(2) = (w1(2) And w2(2)) And &HFF
w1(3) = (w1(3) And w2(3)) And &HFF
And2 = W2N(w1)
End Function
'两个整数逻辑非运算并返回结果
Public Function Not2(ByVal n)
Dim w
w = N2W(n)
w(0) = (Not w(0)) And &HFF
w(1) = (Not w(1)) And &HFF
w(2) = (Not w(2)) And &HFF
w(3) = (Not w(3)) And &HFF
Not2 = W2N(w)
End Function
'两个整数逻辑异或运算并返回结果
Public Function Xor2(ByVal n1, ByVal n2)
Dim w1, w2
w1 = N2W(n1)
w2 = N2W(n2)
w1(0) = (w1(0) Xor w2(0)) And &HFF
w1(1) = (w1(1) Xor w2(1)) And &HFF
w1(2) = (w1(2) Xor w2(2)) And &HFF
w1(3) = (w1(3) Xor w2(3)) And &HFF
Xor2 = W2N(w1)
End Function
Response.Write Join(N2W(185999660), ".") & "<br/>"
Response.Write Join(N2W(4294965248), ".")
Response.Write And2(185999660, 4294965248)
Public Function Or2(ByVal n1, ByVal n2)
Or2 = W2N(OrW(N2W(n1), N2W(n2)))
End Function
Public Function And2(ByVal n1, ByVal n2)
And2 = W2N(AndW(N2W(n1), N2W(n2)))
End Function
Public Function Not2(ByVal n)
Not2 = W2N(NotW(N2W(n)))
End Function
Public Function Xor2(ByVal n1, ByVal n2)
Xor2 = W2N(XorW(N2W(n1), N2W(n2)))
End Function
'**************************
'* FUNCTIONS
'**************************
Function IPToBin(strIP)
' convert IP to 32-bit binary value
strOctets=Split(strIP,".")
strIPbin=""
For Each strOctet in strOctets
intOctet = CInt(strOctet)
sum = ""
result = intOctet
count=0
While result > 0
intMod = result mod 2
sum = CStr(intMod) + sum
result = (result - intMod) / 2
count = count + 1
Wend
strIPbin = strIPbin + String(8 - count,"0") + sum
Next
IPToBin = strIPbin
End Function
Function LongToBin(lngIP)
' convert long IP to 32-bit binary value
strIPbin=""
sum = ""
result = lngIP
count=0
While result > 0
'' mod substitute to avoid overflow on very large value
if instr(CStr(result / 2),".5") > 0 then
intMod = 1
else
intMod = 0
end if
''
sum = CStr(intMod) + sum
result = (result - intMod) / 2
count = count + 1
Wend
LongToBin = strIPbin + String(32 - count,"0") + sum
End Function
Function BinToLong(strIPbin)
' convert 32-bit binary IP to Long
intUBound = Len(strIPbin)
lngIPLong = 0
For i = 0 to intUBound - 1
lngIPLong = lngIPLong + CInt(mid(strIPbin,intUBound - i,1)) * 2^i
Next
BinToLong = lngIPLong
End Function
Function BinToIP(strIPbin)
' convert 32-bit binary IP to IP address
Dim Octet(4)
Set oRegEx = New RegExp
strIPbin = Trim(strIPbin)
oRegEx.Pattern = "^[01]{32}$"
Set oRegExMatch = oRegEx.Execute(strIPbin)
if oRegExMatch.Count = 1 then
Octet(0) = mid(strIPbin,1,8)
Octet(1) = mid(strIPbin,9,8)
Octet(2) = mid(strIPbin,17,8)
Octet(3) = mid(strIPbin,25,8)
BinToIP = BinToLong(Octet(0)) & "." & BinToLong(Octet(1)) & "." & BinToLong(Octet(2)) & "." & BinToLong(Octet(3))
end if
End Function
Function LongToIP(lngIP)
' convert Long IP to IP address
LongToIP = BinToIP(LongToBin(lngIP))
End Function
'****************************************************
'* PROGRAM
'****************************************************
' Author: Matthew Buckley-Golder
' Created: November 6, 2007
' Last Modified: November 6, 2007
'****************************************************
'
' InFile should be a comma-delimited CSV file as follows:
' Column 1: Network ID
' Column 2: Subnet Mask
' * All other columns in InFile will be ignored. If the format of an input line is incorrect, an error message
' will appear for that line within OutFile, but will not abort the program
'
' Modify the value of InFile in the "CONFIGURATION" section below to specify the input file
' Modify the value of OutFile in the "CONFIGURATION" section below to specify the output file
'
'****************************************************
'***************
'* CONFIGURATION
'***************
Const InFile = "D:\in.csv"
Const OutFile = "D:\ip.txt"
'****************
'* MAIN PROGRAM
'***************
Const ForReading = 1
Const OutputDescription = True
Err.Clear
On Error Resume Next
Set oRegEx = New RegExp
Set oFSO = CreateObject("Scripting.FileSystemObject")
' Create output file
Set oOutFile = oFSO.CreateTextFile(OutFile, True)
if Err.Number <> 0 then
MsgBox "Error creating output file: " & OutFile & VbCrLf & VbCrLf & "It may be open in another program, or you may not have permission to create it." & VbCrLf & VbCrLf & "Aborting."
WScript.Quit
end if
' Open input file
Set oInFile = oFSO.OpenTextFile(InFile, ForReading)
if Err.Number <> 0 then
MsgBox "Error opening input file: " & InFile & VbCrLf & VbCrLf & "It may not exist, may be locked by another program, or you may not have permission to read it." & VbCrLf & VbCrLf & "Aborting."
WScript.Quit
end if
On Error Goto 0
Do Until oInFile.AtEndOfStream
arrInputLine = split(oInFile.ReadLine, ",")
strNetwork = Trim(arrInputLine(0))
strSubnet = Trim(arrInputLine(1))
if OutputDescription = True then
strDescription = Trim(arrInputLine(2))
end if
oRegEx.Pattern="^((\d{1,2}|[01]\d{2}|2[0-4]\d|25[0-5])\.){3}(\d{1,2}|[01]\d{2}|2[0-4]\d|25[0-5])$"
if oRegEx.Execute(strSubnet).Count = 1 and oRegEx.Execute(strNetwork).Count = 1 then
SubnetBin = IPToBin(strSubnet)
NetworkBin = IPToBin(strNetwork)
NetworkIDBits = InStrRev(SubnetBin,"1")
HostIDBits = 32 - NetworkIDBits
IPStart = BinToLong(mid(NetworkBin,1,NetworkIDBits) + String(HostIDBits, "0")) + 1
IPFinish = BinToLong(mid(NetworkBin,1,NetworkIDBits) + String(HostIDBits, "1")) - 1
if OutputDescription = True then
for j = IPStart to IPFinish
oOutFile.WriteLine LongToIP(j) & "," & strDescription
next
else
for j = IPStart to IPFinish
oOutFile.WriteLine LongToIP(j)
next
end if
else
oOutFile.WriteLine "Error on input line: Network '" & strNetwork & "'; Subnet '" & strSubnet & "'"
end if
Loop
MsgBox "Processing done." & VbCrLf & VBCrLf & "Output resides in: " & OutFile
WScript.Quit