Private Sub Command1_Click() Dim a As Single Dim b As String Dim x a = -1000100 b = a x = InputBox("请输入位数,1-7") MsgBox Mid(b, x + 1, 1) End Sub
http://www.xbeat.net/vbspeed/c_LongToBit.htm
Private Sub Command1_Click() MsgBox GetBitByPosition(&HF000, 15) End Sub'''取指定位的值,返回1或0 '''lngData 目标数据 '''从右边数起第n位(7,6,5,.....3,2,1,0) '''最高位为7,最低位为0 Private Function GetBitByPosition(ByVal lngData As Byte, ByVal nPos As Long) As Long Dim temp As Long
temp = 2& ^ clng(nPos) temp = lngData And temp If (temp) Then GetBitByPosition = 1 Else GetBitByPosition = 0 End Iflong在内容里就是连着的4个Byte,int就是2个。楼主看着办
如果是指二进制的位的情况下:VB6里面全是有符号数.所以最高位被用来做符号位了,始终为1.其它位,直接与一下就可以了,如下:msgbox -128 and 2^7 返回值为0表示那一位为0,否则为1.式中的7代表第7位,右起第一位为0位.
Dim a As Long, b As Long, c As Longa = -254 b = a And &HFF得到的结果是:b=2 这个结果不对吧。
? -254 and &h100 256
&Hff并不是你要的位。如果你要第7位(从右数,从0起),那么你要与的&H80(2^7,2的7次方), 结果不等于0的就是1,等于0的就是0 x=iif((a and 2^7)>0,1,0)
and运算来检查。对于一个长度为4的二进制数据(如1111),如果检查最高位也就是第4位是否为1那么就和1000去做and运算。 1111 and 1000得出的是1000,最高位是1。如果是 0101 and 1000得出的是0000,那么最高位就会被判断出是0。上面的检测第4位是否为1就是和10的3次方做and运算,也就是检测数字n的m位的算法就是n and 10^(m-1)
Dim a As Single
Dim b As String
Dim x
a = -1000100
b = a
x = InputBox("请输入位数,1-7")
MsgBox Mid(b, x + 1, 1)
End Sub
Private Sub Command1_Click()
MsgBox GetBitByPosition(&HF000, 15)
End Sub'''取指定位的值,返回1或0
'''lngData 目标数据
'''从右边数起第n位(7,6,5,.....3,2,1,0)
'''最高位为7,最低位为0
Private Function GetBitByPosition(ByVal lngData As Byte, ByVal nPos As Long) As Long
Dim temp As Long
temp = 2& ^ clng(nPos)
temp = lngData And temp
If (temp) Then
GetBitByPosition = 1
Else
GetBitByPosition = 0
End Iflong在内容里就是连着的4个Byte,int就是2个。楼主看着办
返回值为0表示那一位为0,否则为1.式中的7代表第7位,右起第一位为0位.
Dim a As Long, b As Long, c As Longa = -254
b = a And &HFF得到的结果是:b=2 这个结果不对吧。
256
结果不等于0的就是1,等于0的就是0
x=iif((a and 2^7)>0,1,0)
1111 and
1000得出的是1000,最高位是1。如果是
0101 and
1000得出的是0000,那么最高位就会被判断出是0。上面的检测第4位是否为1就是和10的3次方做and运算,也就是检测数字n的m位的算法就是n and 10^(m-1)