直接取字节的某一位的算法
vb吧
全部回复
仅看楼主
level 1
直接取出指定字节(Byte)的某一位(Bit)的值,结果只会是0或1,也就是想知道这个位上是0还是1,大家有什么优的算法,不妨帖出来供大家研究和学习!我这里先帖出我的算法,经测试,第一种方法比第二种方法在速度上要快一半有多,很明显,第一种方法,用的是常量,所以少了计算步骤,而第二种方法还要先计算幂,所以就慢了许多,但从代码量上来讲,第二种方法较简捷。Option ExplicitDim byts(0 To 255) As BytePrivate Sub Command1_Click()  Dim ts As Single, td As Single  Dim i As Integer, j As Long, k As Integer, b As Byte  ts = Timer  For j = 0 To 1000    For i = 0 To 255'      Debug.Print byts(i),      For k = 7 To 0 Step -1'        Debug.Print ByteBit(byts(i), k),'        Debug.Print ByteBitEx(byts(i), k),'        b = ByteBit(byts(i), k)        b = ByteBitEx(byts(i), k)      Next'      Debug.Print "================================================"    Next  Next  td = Timer  Text1.Text = ts  Text2.Text = td  Text3.Text = td - ts  Debug.Print Now, td - tsEnd SubPrivate Sub Form_Load()  Dim i As Integer  For i = 0 To 255    byts(i) = i  NextEnd Sub'取字节的某一位Private Function ByteBit(ByVal bytValue As Byte, ByVal intBitIndex As Integer) As Byte  Select Case intBitIndex  Case 0    ByteBit = bytValue And &H1  Case 1    ByteBit = (bytValue And &H2) \ &H2  Case 2    ByteBit = (bytValue And &H4) \ &H4  Case 3    ByteBit = (bytValue And &H8) \ &H8  Case 4    ByteBit = (bytValue And &H10) \ &H10  Case 5    ByteBit = (bytValue And &H20) \ &H20  Case 6    ByteBit = (bytValue And &H40) \ &H40  Case 7    ByteBit = (bytValue And &H80) \ &H80  End SelectEnd Function'取字节的某一位Private Function ByteBitEx(ByVal bytValue As Byte, ByVal intBitIndex As Integer) As Byte  Select Case intBitIndex  Case 0 To 7    ByteBitEx = (bytValue And 2 ^ intBitIndex) \ 2 ^ intBitIndex  End SelectEnd Function以下是测试结果:调用ByteBit07-04-12 01:32:27 2.015625 07-04-12 01:32:31 2.202637 07-04-12 01:32:34 2 07-04-12 01:32:37 2.016113 07-04-12 01:32:40 2 07-04-12 01:32:43 2.015625 07-04-12 01:32:46 2 07-04-12 01:32:49 2.016113 07-04-12 01:32:55 1.983887 07-04-12 01:32:59 1.984863 07-04-12 01:33:03 2 07-04-12 01:33:06 2.015137 =====================================调用ByteBitEx07-04-12 01:34:41 4.219238 07-04-12 01:34:47 4.219238 07-04-12 01:34:52 4.203125 07-04-12 01:34:57 4.203125 07-04-12 01:35:02 4.21875 07-04-12 01:35:07 4.202637 07-04-12 01:35:11 4.219238 07-04-12 01:35:15 4.203125 07-04-12 01:35:19 4.219238 07-04-12 01:35:23 4.21875 07-04-12 01:35:28 4.219238 
2007年04月11日 17点04分 1
level 1
希望大家是在b = ByteBit(byts(i), k)过程里面求最优的算法,而不应该在Private Sub Command1_Click()过程里做文章,为了保持公平起见,请直接使用这个“Command1_Click()”的原代码来测试,谢谢!
2007年04月11日 17点04分 2
level 1
睡觉去了,改天再说吧
2007年04月11日 18点04分 4
level 0
好吧。
2007年04月11日 22点04分 5
level 12
Option ExplicitDim byts(0 To 255) As ByteDim myKey(7) As BytePrivate Sub Command1_Click() Dim ts As Single, td As Single Dim i As Integer, j As Long, k As Integer, b As Byte ts = Timer For j = 0 To 1000 For i = 0 To 255 For k = 7 To 0 Step -1 b = ByteBitEx3(byts(i), k) Next Next Next td = Timer Text1.Text = ts Text2.Text = td Text3.Text = td - ts Debug.Print Now, td - tsEnd SubPrivate Sub Form_Load() Dim i As Integer For i = 0 To 255 byts(i) = i Next myKey(0) = &H1 myKey(1) = &H2 myKey(2) = &H4 myKey(3) = &H8 myKey(4) = &H10 myKey(5) = &H20 myKey(6) = &H40 myKey(7) = &H80End Sub'取字节的某一位Private Function ByteBit(ByVal bytValue As Byte, ByVal intBitIndex As Integer) As Byte Select Case intBitIndex Case 0 ByteBit = bytValue And &H1 Case 1 ByteBit = (bytValue And &H2) \ &H2 Case 2 ByteBit = (bytValue And &H4) \ &H4 Case 3 ByteBit = (bytValue And &H8) \ &H8 Case 4 ByteBit = (bytValue And &H10) \ &H10 Case 5 ByteBit = (bytValue And &H20) \ &H20 Case 6 ByteBit = (bytValue And &H40) \ &H40 Case 7 ByteBit = (bytValue And &H80) \ &H80 End SelectEnd Function'取字节的某一位Private Function ByteBitEx(ByVal bytValue As Byte, ByVal intBitIndex As Integer) As Byte Select Case intBitIndex Case 0 To 7 ByteBitEx = (bytValue And 2 ^ intBitIndex) \ 2 ^ intBitIndex End SelectEnd Function'取字节的某一位(ggggdiu)Private Function ByteBitEx3(ByVal bytValue As Byte, ByVal intBitIndex As Integer) As Byte ByteBitEx3 = bytValue And myKey(intBitIndex)End Function
2007年04月11日 23点04分 6
level 1
楼主,我没有看你的算法不过我有个算法,个人觉得应该是比较快的,在这里我只说一下算法原理,至于具体的算法,如果需要的话,我再写算法原理如下:大家都知道,数字最终都可以用0和1的组合表示出来,如果想知道一个byte类型的数某一位是0还是1,可以这样做,因为byte在VB中只占1个字节,所以如果想让知道比如某个数字的第3位是否为1,我只需要构造一个数字,其他的位都为0,第三位为1,然后那构造的这个数字和原来的那个数字进行and操作:如果原来的数字第三位为0,那么and后,就是0,如果第三位为1,那么and出来的结果就是1,而and操作大家都应该知道速度是如何的快了吧,至于构造的数字,那也非常简单而且只需自己去构造,计算机并不参与进来,那么速度大家可想而知
2007年04月12日 00点04分 7
level 1
请大家注意一个问题,那就是Byte And Byte,如果相同位上不同,则返回0,如果相同位上相同,则返回相同位,即:0001 0000b And 0001 0000b返回的是0001 0000b,而不是0000 0001b!所以,楼上ggggdiu的ByteBitEx3(&H8, 3)返回结果为&H8,而不是1
2007年04月12日 00点04分 8
level 1
当然可以再根据返回的结果来判断,如果为0则说明指定位上为0,否则说明指定位上为1,但这还需要一次判断处理,可能是各位还没理解我的意思,我的意思是,给你一个字节,和一个数字,数字代表指定位的索引号,你只要告诉我这个位上是0,还是1,返回结果只可能是0或1,用个通俗的示例来说明:字节0x42,它的二进制表示为0100 0010b,那么我问第1位(位的索引为0)上是多少,回答是0第2位(位的索引为1)上是多少,回答是1第3位(位的索引为2)上是多少,回答是0第4位(位的索引为3)上是多少,回答是0第6位(位的索引为5)上是多少,回答是0第7位(位的索引为6)上是多少,回答是1。。。大家应该明白了吧?
2007年04月12日 00点04分 9
level 0
直接用AND就行了,API就是这样判断的。
2007年04月12日 01点04分 10
level 1
我再补充一点,字节0100 0010b,当我们只看某一位(不看其它位)的时候,我们就只能看到一个0,或是一个1,我的意思就是这个意思!当我要看第一位的时候(位的索引为0),它是xxxx xxx0,只看到0 当我要看第二位的时候(位的索引为1),它是xxxx xx1x,只看到1 当我要看第三位的时候(位的索引为2),它是xxxx x0xx,只看到0 当我要看第四位的时候(位的索引为3),它是xxxx 0xxx,只看到0 当我要看第六位的时候(位的索引为5),它是xx0x xxxx,只看到0 当我要看第七位的时候(位的索引为6),它是x1xx xxxx,只看到1。。。
2007年04月12日 01点04分 11
level 0
直接用AND就行了,API就是这样判断的
2007年04月12日 01点04分 12
level 1
哎,算我没说!不知道是我表达不清,还是你理解错误?
2007年04月12日 01点04分 13
level 1
谢谢大家参与!
2007年04月12日 01点04分 14
level 0
字节就是8位而已,Function is1(b As Byte, n As Byte) As Boolean If b And (2 ^ (n - 1)) Then is1 = TrueEnd Function
2007年04月12日 01点04分 15
level 12
呵呵,疏忽了,不过一般返回值是bool的,这样是没有什么问题的。
2007年04月12日 01点04分 16
level 12
'取字节的某一位(ggggdiu)Private Function ByteBitEx3(ByVal bytValue As Byte, ByVal intBitIndex As Integer) As Byte ByteBitEx3 = ((bytValue And myKey(intBitIndex)) <> 0)End Function这样改改也行的。
2007年04月12日 01点04分 17
level 1
这里是在研究和讨论算法,可不可以实现,大家可以写出实际代码,并测试通过,第一要求是能出
正确的
结果,以下是测试结果是否正确的测试代码:For i = 0 To 255   Debug.Print byts(i),   For k = 7 To 0 Step -1     Debug.Print ByteBit(byts(i), k),   Next   Debug.Print "" Next 如果满足我要求,那么正确的结果应该是:0   0 0 0 0 0 0 0 01   0 0 0 0 0 0 0 12   0 0 0 0 0 0 1 0...253 1 1 1 1 1 1 0 0254 1 1 1 1 1 1 1 0255 1 1 1 1 1 1 1 1
2007年04月12日 02点04分 18
level 1
更正一下253 1 1 1 1 1 1 0 1
2007年04月12日 02点04分 19
level 0
Function is1(b As Byte, n As Byte) As Boolean If b And (2 ^ (n - 1)) Then is1 = True End Function 这不是代码吗?
2007年04月12日 02点04分 20
1 2 3 尾页