我到这里不是为了争分。只是觉得这里有很多热情的、专业的、称职的老师。
所以这两天连着问问题。在这里问比在msdn上查还有针对性。vb能够像汇编那样,对一个字节进行拆分(高四位、低四位)、对一个字节进行左、右移位、与、或、非、异或吗?先谢谢了!

解决方案 »

  1.   

    And &HF0    '取高4位
    And &H0F    '取低4位
    * 2         '左移1位
    \ 2         '右移1位
    And         '与
    OR          '或
    Not         '非
    Xor         '异或这些都是基础知识
      

  2.   

    能够拆分出高4位、低四位:
    C8h ----> H = &HC8 \ 16   L = &HC8 And 15移位:
    只有用乘除法来间接实现,因为存在溢出问题,相对麻烦些。
    但你说只对一个字节进行移位,那就没问题了。用Long变量来做中介,没任何麻烦。
    右移,用除法:
    C8h >> 3 ----> &HC8 \ 8
    左移,用乘法:
    C8h << 2 ----> (&HC8 * 4) And &Hff与、或、非、异或:
    与 And  ---->  &HC8 And 15   (= 8)
    或 Or   ---->  &HC8 Or 15    (= &HCF)
    非 Not
    Dim bNum As Byte
    bNum = &HC8
    bNum = Not bNum
       (bNum = &H37)
    异或 Xor  ----> &HC8 Xor &H63 ( = &HAB)
      

  3.   

    我用vb都是处理文件和字符串比较多,很少有到这些。
    这样的工作一般都用单片机搞。
    你们提供的例子的确在书上看过,自己没有这磨用过。哪位给提供一个能运行的完整的例子?就作为我今后的典型教材了。
    我有点怕vb的变量!尤其是byte和什莫转换来、转换去的。
    多谢了!
      

  4.   

    问出这么专门的问题,却不肯自己动手写个SUB,然后在立即窗口试试结果?
      

  5.   

    slograce谢谢你的教诲。但是我的确不是懒惰。有些vb的基本概念没有搞清楚。想在这里系统的学一下。
    另外书上这方面的介绍也比较少。而且这些内容我以后可能要用到。看一下我给用户写的报告http://www.cncb3h.com/CHARMILLES_rpt.htm这个就是我做的项目。主要目的就是将数控系统磁带机的磁带里面的内容给掏出来,存入电脑。再用电脑模拟磁带机将数据传入数控系统。由于vb学得不好,当时用户又急等着要,只能用qbasic搞定。不过那台数控系统是瑞士20年前的设备,用dos也对的起它。否则这台一百多万的设备就报废了。
    另外本人的确不是专门做软件的。电路设计、单片机设计、plc设计。软件只是我的付业。另外最近有点忙!Sincerely & Thanks
      

  6.   


    我不太明白,我是个vb白痴,不过我知道,google上搜索“vb 移位 操作”,你看看有多少个现成的答案?看你打这么多字,的却不懒惰,那难道是你不会用google?
      

  7.   

    也许像我这样按部就班学东西的人不适合现在这个时代了吧?不过,如果你要写的东西要用于实际产品的话,建议还是认真看一下MSDN的程序员手册的Part One,讲得很基础。如果什么都不懂,就拷贝一些勉强理解了的代码到产品里去,总觉得对产品不够负责。
      

  8.   

    另外,在VB里变量可以不声明类型,但是并不是best practices所推荐的,所以你完全可以不理会这种不声明的变量所带来的灵活性或复杂性。
      

  9.   

    slowgrace感谢你的教诲。我知道有我们在这里讨论的功夫,如果按照上面人的热心指点我也试出来了。
    我只是想看一下高手们写的漂亮语句和高手处理问题的思维方式。另外,我只是一年使用一两次vb,和软件高手们的确差得太远。我到现在还没有感觉到主动和高手学习点经验有什莫不对。另外,vb里除了\2 和×2就没有别的招了吗?类似于
     rol a
     ror a
    的语句吗。这两天有点烦,不想自己试了。如果这里实在没人出手。那天我有时间,一定按照你的意思和上面的人的指点去做实验。多谢!
      

  10.   

    对于左移,右移VB没有直接的指令实现,Delphi有
      

  11.   

    难道没有第三方的控件实现这个功能吗,类似像vb可以直接控制打印口(centronic)一样。乘除法看着很难受啊!多谢各位!
      

  12.   

    写个 过程/函数 就很难吗?楼主何必为自己的懒找借口呢!
    这么点功能都要用控件?真不知道楼主在怎么想。你不就是要对 字节 进行移位操作吗,简单得很,给你段代码看看吧:
    Option Explicit
    ' Byte 左移位
    Private Sub rol(n As Byte, Optional ByVal bit As Long = 1)
        If (bit < 0) Then Err.Clear: Err.Raise 6
        If (bit > 7) Then
            n = 0
        Else
            n = CLng(n) * 2& ^ bit And 255
        End If
    End Sub
    ' Byte 右移位
    Private Sub ror(n As Byte, Optional ByVal bit As Long = 1)
        If (bit < 0) Then Err.Clear: Err.Raise 6
        If (bit > 7) Then
            n = 0
        Else
            n = CLng(n) \ 2& ^ bit
        End If
    End SubPrivate Sub Command1_Click()
    ' 测试代码:
        Dim a As Byte
        a = 13:     Cls
        rol a:      Print a
        rol a, 4:   Print a
        ror a:      Print a
        ror a, 3:   Print a
        ror a, 100: Print a
        
    End Sub
      

  13.   

    n = CLng(n) \ 2& ^ bit
    这句改成:n = n \ 2& ^ bit
      

  14.   

    http://www.bitwisemag.com/2/Bit-Shifting-in-Visual-Basic-6
    我在国外的网页上摘了一段过来。原文点击上述网址
    c = inputValue(0) And &HC0000000
    inputValue(0) = inputValue(0) And &H3FFFFFFF
    inputValue(0) = inputValue(0) * 2' rotate Most Significant Bit (MSB) into Least Significant Bit (LSB)
    If c And &H80000000 Then
        inputValue(0) = inputValue(0) Or 1
    End If' set Most Significant Bit
    If c And &H40000000 Then
        inputValue(0) = inputValue(0) Or &H80000000
    Else
        inputValue(0) = inputValue(0) And &H7FFFFFFF
    End If我觉得这不是我这样的水平能想得到的。下面是源码:(上面的网站可以下载完整的)
     '
     ' Visual Basic Sample Code provided by BITWISE MAGAZINE
     '
     '      http://www.bitwisemag.com
     '
     ' This code and information is provided "as is" without warranty of
     ' any kind, either expressed or implied, including but not limited
     ' to the implied warranties of merchantability and/or fitness for a
     ' particular purpose.
     '
     ' You may use and modify this code for your personal use only.
     ' You may also redistribute this code providing that
     ' a) No fee is charged
     ' b) This copyright notice is retained in the source code.
     '
     ' You may not use this code for commercial purposes
     ' without the express permission of the copyright holder
     '
     ' Copyright (c) 2006 Dermot Hogan
     '
     '
    Option Explicit
    Dim shiftValue As Integer
    Dim inputValue(1) As Long
    Dim outputValue(1) As Long
    Dim s As String
    Dim slen As Integer
    Dim upper, lower As String
    '
    ' this doesn't do any serious checking of the input
    ' but it does check for -ve numbers
    Private Sub ShiftButton_Click()
       
        shiftValue = Val(ShiftText.Text)
        s = InputText.Text
        If Left(s, 1) = "-" Then
            MsgBox ("positive numbers only!")
            Exit Sub
        End If
        
        slen = Len(s)
        
           
        inputValue(0) = 0
        inputValue(1) = 0
        lower = s
        If Len(s) > 8 Then
            upper = Mid(s, 1, 8)
            inputValue(1) = Val("&H" + upper)
            lower = Mid(s, 9, slen - 8)
        End If
        inputValue(0) = Val("&H" + lower)
        
        Rotate64 (shiftValue)
        
        OutputText.Text = Format((Hex(inputValue(1))), "00000000") & Format(Hex(inputValue(0)), "00000000")
        
     End Sub
    ' shift left or right by upto 32 bits
    Sub Rotate32(bits As Integer)
        Dim i As Integer
        Dim c As Long
        
        If (bits > 0) Then
        
            ' we need to shift left by one bit
            ' but we have to take account of overflow error from VB
            ' so mask accordingly
            For i = 1 To shiftValue
                c = inputValue(0) And &HC0000000
                inputValue(0) = inputValue(0) And &H3FFFFFFF
                inputValue(0) = inputValue(0) * 2
                ' rotate MSB into LSB
                If c And &H80000000 Then
                    inputValue(0) = inputValue(0) Or 1
                End If
                ' set or clear MSB
                If c And &H40000000 Then
                    inputValue(0) = inputValue(0) Or &H80000000
                Else
                    inputValue(0) = inputValue(0) And &H7FFFFFFF
                End If
            
            Next
        Else
            For i = bits To -1
                c = inputValue(0) And &H80000001
                inputValue(0) = inputValue(0) And &H7FFFFFFF
                inputValue(0) = inputValue(0) \ 2
                ' rotate MSB into LSB
                If c And 1 Then
                    inputValue(0) = inputValue(0) Or &H80000000
                End If
                ' set or clear the old sign bit
                If c And &H80000000 Then
                    inputValue(0) = inputValue(0) Or &H40000000
                Else
                    inputValue(0) = inputValue(0) And &HBFFFFFFF
                End If
            Next
        End IfEnd Sub
    ' shift left or right by upto 64 bits
    Sub Rotate64(bits As Integer)
        Dim i As Integer
        Dim c(1) As Long
        
        If (bits > 0) Then
        
            ' we need to shift left by one bit
            ' but we have to take account of overflow error from VB
            ' so mask accordingly
            For i = 1 To shiftValue
                c(0) = inputValue(0) And &HC0000000
                c(1) = inputValue(1) And &HC0000000
                inputValue(0) = inputValue(0) And &H3FFFFFFF
                inputValue(0) = inputValue(0) * 2
                inputValue(1) = inputValue(1) And &H3FFFFFFF
                inputValue(1) = inputValue(1) * 2
                ' rotate MSB into LSB
                If c(1) And &H80000000 Then
                    inputValue(0) = inputValue(0) Or 1
                End If
                ' set MSB
                If c(1) And &H40000000 Then
                    inputValue(1) = inputValue(1) Or &H80000000
                Else
                    inputValue(1) = inputValue(1) And &H7FFFFFFF
                End If
                If c(0) And &H80000000 Then
                    inputValue(1) = inputValue(1) Or 1
                End If
                ' set MSB
                If c(0) And &H40000000 Then
                    inputValue(0) = inputValue(0) Or &H80000000
                Else
                    inputValue(0) = inputValue(0) And &H7FFFFFFF
                End If
            Next
        Else
            For i = bits To -1
                c(0) = inputValue(0) And &H80000001
                c(1) = inputValue(1) And &H80000001
                inputValue(0) = inputValue(0) And &H7FFFFFFF
                inputValue(1) = inputValue(1) And &H7FFFFFFF
                inputValue(0) = inputValue(0) \ 2
                inputValue(1) = inputValue(1) \ 2
                ' rotate MSB into LSB
                If c(0) And 1 Then
                    inputValue(1) = inputValue(1) Or &H80000000
                End If
                If c(1) And 1 Then
                    inputValue(0) = inputValue(0) Or &H80000000
                End If
                ' set or clear the old sign bit
                If c(0) And &H80000000 Then
                    inputValue(0) = inputValue(0) Or &H40000000
                Else
                    inputValue(0) = inputValue(0) And &HBFFFFFFF
                End If
                If c(1) And &H80000000 Then
                    inputValue(1) = inputValue(1) Or &H40000000
                Else
                    inputValue(1) = inputValue(1) And &HBFFFFFFF
                End If
            Next
        End IfEnd Sub我看人家那里讨论得挺欢的!!!!????
    我认为基础是最重要的。这两天在这里学习了两天,解决了长期困扰我的问题。
    再次感谢各位任何方式的帮助!
      

  15.   

    There are actually two options: either throw it away or ‘rotate’ it into the top or bottom of the word. Here, I’ll ‘rotate’ the bit.
    看来不单是可以移位,而且还可以旋转!
      

  16.   

    我在上面的程序里试了一下:
    在 input 框里输入正值,例如:24在shift 框里输入 1   output 框显示 48
    在shift 框里输入 -1   output 框显示 12
      

  17.   

    LZ在14楼说:乘除法看着很难受啊!
    而LZ在18楼贴出的http://www.bitwisemag.com/2/Bit-Shifting-in-Visual-Basic-6下的代码充满了3种运算: Or运算 And运算 /运算,那就看着不难受了?
    以下简摘自MSDN:
    Or 运算符
    用来对两个表达式进行逻辑析取运算。
    Or 运算符也对两个数值表达式中位置相同的位进行逐位比较,并根据下表对 result 中相应的位进行设置And 运算符
    用来对两个表达式进行逻辑连接。
    And 运算符还对两个数值表达式中位置相同的位进行逐位比较,并根据下表对 result 中相应的位进行设置
      

  18.   

    对byte类型变量做位移,可以使用查表法
    对长整型变量就只能老老实实做乘除法了
      

  19.   

    1024 已经不是 Byte类型数据 能表示的了,如果仅限于 Byte类型的 移位,我在 15F 的代码足够用。
      

  20.   

    Chen8013我刚看到你的大作,真给我面子。
    说句实话,你编的程序,让我编十天我也想不出来。
    Optional ByVal bit As Long = 1
    这种语句我从来没用过。就算你写出来我还得找地方查一下啥意思呢!三人行必有我师,这里得有多少老师啊。thank you for any help!
      

  21.   

    左移其实就是对Byte类型的数据 X 2  截去超出256的部分
    左移其实就是对Byte类型的数据 / 2  
      

  22.   

    ch8013刚试了一下你的代码。可读性和使用的语句比我强多了。我得好好学一下。thanks for any help!
      

  23.   

    阿根廷巫师你是巫师级的,能给演示一个不截取的,再转回来。搞个旋转的。
    我看www.denfordata.com 起名为:guru的都是高手中的高手。
    开个玩笑,没时间就算了。thank you for any help!
      

  24.   

    你要循环移位?
    rol 、ror 似乎是循环移位啊。你在主贴中可说的是左移、右移(我理解是单向移位),因此我贴的代码是单向移位的。有单向移位的代码,循环移位也是很容易实现的。
      

  25.   

    VB boolean operators in order of precedence:Operator -- Operation:   1. Not -- negation
       2. And -- conjunction
       3. Or -- disjunction
       4. Xor -- exclusive disjunction
       5. Eqv -- equivalent
       6. Imp -- implies The boolean operators are lower in precedence than the arithmetic operators and the comparison operators. The boolean constants True and False may also be considered operators with 0 operands.Truth Tables:
    A  B A And B A Or B A Xor B A Eqv B A Imp BT  T    T    T    F    T    T
    T  F    F    T    T    F    F
    F  T    F    T    T    F    T
    F  F    F    F    F    T    T听了slowgrace的话,又开始网上搜去了。虽然我是搞硬件出身的, imp 用的少,还真给忘了。
      

  26.   

    这个贴子不白顶
    第一次知道VB还有eqv imp这两个运算符
    得琢磨琢磨在哪里能用上这两的东东
      

  27.   

    从学习的角度来说,我喜欢先在天上转一圈,再蹲到井底下当一阵子青蛙,之后再跑河边歇一阵子。要想编好的程序,首先要多看高手编的程序。明早还要跑几百里路做数控机床改造的调研,祝各位晚安!thanks for any help!
      

  28.   

    我杀回来了!今早在窦店北京和河北的交界处,让我加97号油(应该用93)宰了我一刀。回京时,在涿州松林店镇加上93号了(中石油加油站),说是带乙醇的(6.03元/升)。这个价好像也比美国的贵呀,怎么还带乙醇。又被坑了一把(据说毁车)。看来出门前真是别嫌来不及了,加满油再走吧!看到myjian是我的同行、slowgrace又来关心一下、clear zero 也来学、一天的劳累一扫光。
      

  29.   


    这些做法都是可以,关键VB里面很少需要有这方面的需要,至少我做这么多年VB方面的软件开发,都没有应用到。
      

  30.   

    Simple Bit Manipulation for VBAuthor:  Theo Ekelmans
    Category:  Miscellaneous
    Type:  Snippets
    Difficulty:  IntermediateVersion Compatibility:   Visual Basic 5   Visual Basic 6More information: VB lacks bit manipulation, these 4 function provide basic bitmanipulation.This code has been viewed 63905 times.Instructions: Copy the declarations and code below and paste directly into your VB project.
    Declarations:
    no
    ----------------------------------------------------------------------
    Public Function SetBit(InByte As Byte, Bit As Byte) As Byte
    'Set het n'de bit of van InByteSetBit = InByte Or (2 ^ Bit)  'Set het n'de BitEnd Function
    Public Function ClearBit(InByte As Byte, Bit As Byte) As Byte
    'Clear het n'de bit of van InByte
       
    ClearBit = InByte And Not (2 ^ Bit) 'Clear het n'de BitEnd FunctionPublic Function IsBitSet(InByte As Byte, Bit As Byte) As Boolean
    'Is het n'de bit van InByte gezet of niet?IsBitSet = ((InByte And (2 ^ Bit)) > 0)End Function
    Public Function ToggleBit(InByte As Byte, Bit As Byte) As Byte
    'Toggle'ed het n'de van InByteToggleBit = InByte Xor (2 ^ Bit)End Function
    ------------------------------------------
     在“英语 > 中文(简体)”字典中找到。het 发音 [美] [hɛt] [英] [het]    *
             1. = hethhet 发音 [美] [hɛt] [英] [het]    *
             1. (【方】 heat 的过去式与过去分词)
    在“英语 > 中文(简体)”字典中找到。heth 发音 [美] [keɵ] [英] [keiɵ]    *
          名词
             1. 希伯来语的第八个字母自学一下。
      

  31.   

    slowgrace 你说的 MSDN的程序员手册 (msdn programmer manual 对吗?) 网上没有啊。能给提供个的网址吗。
    我现在有点想看书了。。什莫是 2^bit 没印象。发到我信箱也可以:[email protected] for any helps
      

  32.   

    chinasoe 谢谢你的指导。
    你说:“关键VB里面很少需要有这方面的需要,至少我做这么多年VB方面的软件开发,都没有应用到。”可能你们只做单纯的软件。这些在自动化领域是家常便饭。
    例如:用打印机的centronic接口控制外部的硬件。
    我举个具体的例子:你知道 mach 3吗?这是美国一帮业余专家为家庭数控机床设计的系统。它通过打印口可以控制三台步进电机或交流伺服电机,工作在脉冲串方式。现在这个系统已经基本普及到中国的中学了。每套软件卖300美元。对于每套10几万元的标准数控系统来说,就是白给了。注意:mach 3是在世界范围卖。全世界又有都少中学呢?它可以控制刀具走直线、斜线、圆弧、非园曲线、电机的加速和减速控制。以及刀具加工磨损后的补偿。我说的只是1/10之一的功能。所以说二进制处理对于搞控制是离不开的。
    我体会:如果有70分的专业知识+30分的软件能力,就可以搞出别人没有的东西。或者吹一下,叫做多领域的无缝连接。
    你在 www.yahoo.com 搜 cnc magnetic tape retrofit.全世界有几个人再做?
    现在做纯软件的大把抓,有的是。但是,做纯软件也是有出路的,例如 ultraedit 一个卖几十欧元也在全世界卖。但是如果和某种专业联系起来,软件可以卖较高的价格。例如:法国的三坐标测量机软件一套卖到十几万。一套cam软件可以卖到几十万。
    瞎说了!不好意思。thanks for any help
      

  33.   

    eqv 即逻辑代数中的所谓“同或”,即 对应位相同则为1,不同则为0,与 Xor(“异或”)刚好相反,
    eqv <-> Not(Xor)imp 是所谓的逻辑“蕴涵”,以前在逻辑代数里看过,但时间太久了,都忘了。刚才看了一下 VB 文档里对此操作符的说明,应该是这样:
    Oprand1 imp Oprand2
    0<->0
    1<->1
    Null<->Null
    0 ->Null->1
    这里,0 与False对应,1与True对应;Oprand1->Oprand2表示Oprand1蕴涵于Oprand2,Oprand1<->Oprand2表示Oprand1与Oprand2彼此蕴涵,即相等。
    如果用True和False来写上面的几个蕴涵关系式,则为
    False<->False
    True<->True
    Null<->Null
    False->Null->True
    如果其中一个操作数为Null,而蕴涵关系不成立(即不是上面的任何一种情况,因而结果不为True),则结果为Null,其它情况结果都为 True/False。各位可对照开发文档和真值表来进行验证。
      

  34.   

    更正:Null imp Null = Null
      

  35.   

    谢谢 zhiyongtu 的认真协助。昨天跑了一天累了,睡的有点早,现在有起早了。发现了个 vb help 网站不错
    http://www.vb-helper.com/index_categories.html对于我这样的业余水平的可能有点用。先放在这里存着。
      

  36.   

    用乘法实现左移可能导致溢出 
    可以设置工程属性,在优化里面选中“取消整数溢出检查”=====================================================================================溢出是可以预防的:a = (a And &H7F) * 2a = (a And &H3F) * 4......
      

  37.   

    如果 a 是 Integer类型的数据:
    当 a≥ 16384 时,(a And &H7F) * 2 就溢出了。
    当 a≥ 8192 时, (a And &H7F) * 4 就溢出了。
      

  38.   

    如果 a 是 Integer类型的数据: 
    当 a≥ 16384 时,(a And &H7F) * 2 就溢出了。 
    当 a≥ 8192 时, (a And &H7F) * 4 就溢出了。 ============================================================Integer 类型是字节吗?楼主的题目是“对一个字节进行左、右移位、与、或、非、异或”。Integer 类型当然还有它的防止溢出方法。不喜欢这样的挑刺。
      

  39.   


    另外,不解的是“当 a≥ 16384 时,(a And &H7F) * 2 就溢出了。”是如何溢出的。16384 And &H7F = 0
    0 * 2 = 08192 And &H3F = 0
    0 * 4 = 0
      

  40.   

    To: of123
    抱歉!把 &H7F 考虑成 &H7FFF 了。