第一步,先把整形變數數(有正有負)轉化為2進位制數存放在一個Byte(0 to 1)中
Dim MyB(1) as byte
if N> =0 then
Myb(0)= N \ 256 //高位
Myb(1)= N mod 256 //低位
else
tmp=N*(-1)
Myb(0)= tmp \256
Myb(1)= tmp mod 256
Myb(0)= Not(Myb(0) and &H80) //將符號位取反
Myb(1)= Not(Myb(1)) +1
//以上兩行是先求出正數N的二進位制表示方法,然後把最高位置1(這樣就是-N的原碼了)
//然後把所有位都取反,末尾+1,這樣就是-N的補碼形式了,在計算機中負數是補碼來存放的。
end if
轉化好以後就是移位操作了,意味操作程式碼如下(假定你要移動N位)
For i=1 to N
SaveHi = Myb(0) "儲存高位,是為了判斷右移時是否向低位位元組進1
"以下是右移 " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " "
Myb(0) = Myb(0) \ 2 "右移不會溢位的
Myb(1)= Myb(1) \ 2
If ((SaveHi And &H1) = &H1) Then
Myb(1) = Myb(1) Or &H80
End If
Next
如果是左移,也是一樣:
SaveLo = Myb(1) "儲存低位,是為了判斷左移時是否向高位位元組進1
Myb(0) = (Myb(0) * 2) mod &HFF "要取餘,不然會溢位
Myb(1) = (Myb(1) * 2) mod &HFF
If ((SaveLo And &H80) = &H1) Then
Myb(0) = Myb(0) Or &H01 "高位進一
第一步,先把整形變數數(有正有負)轉化為2進位制數存放在一個Byte(0 to 1)中
Dim MyB(1) as byte
if N> =0 then
Myb(0)= N \ 256 //高位
Myb(1)= N mod 256 //低位
else
tmp=N*(-1)
Myb(0)= tmp \256
Myb(1)= tmp mod 256
Myb(0)= Not(Myb(0) and &H80) //將符號位取反
Myb(1)= Not(Myb(1)) +1
//以上兩行是先求出正數N的二進位制表示方法,然後把最高位置1(這樣就是-N的原碼了)
//然後把所有位都取反,末尾+1,這樣就是-N的補碼形式了,在計算機中負數是補碼來存放的。
end if
轉化好以後就是移位操作了,意味操作程式碼如下(假定你要移動N位)
For i=1 to N
SaveHi = Myb(0) "儲存高位,是為了判斷右移時是否向低位位元組進1
"以下是右移 " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " "
Myb(0) = Myb(0) \ 2 "右移不會溢位的
Myb(1)= Myb(1) \ 2
If ((SaveHi And &H1) = &H1) Then
Myb(1) = Myb(1) Or &H80
End If
Next
如果是左移,也是一樣:
For i=1 to N
SaveLo = Myb(1) "儲存低位,是為了判斷左移時是否向高位位元組進1
"以下是右移 " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " "
Myb(0) = (Myb(0) * 2) mod &HFF "要取餘,不然會溢位
Myb(1) = (Myb(1) * 2) mod &HFF
If ((SaveLo And &H80) = &H1) Then
Myb(0) = Myb(0) Or &H01 "高位進一
End If
Next