DELPHI6中有没有循环左移的函数,我的表述可能不清楚,就是这个数据左边移出的位值依次放置到此数的右边去。

解决方案 »

  1.   

    两个问题:
        1、得看你是移动的单位,是一次移动一个字节和还是一次只移一个bit
        2、移动数据的数据类型是什么呢?
      

  2.   

    有 shl  这个不是循环移位  但是可以写个函数将之改成不就行了?
      

  3.   

    代码1:function IntRol(v: LongWord): LongWord;
    asm
      bsr   ecx, eax
      xor   ecx, 31
      xor   edx, edx
      inc   ecx
      shld  edx, eax, cl
      shl   eax, cl
      dec   cl
      shr   eax, cl
      or    eax, edx
    end;代码2:function IntRol(v: LongWord): LongWord;
    asm
      push  eax
      bsr   ecx, eax
      mov   edx, 1
      shl   edx, cl
      not   edx
      and   eax, edx
      pop   edx
      shl   eax, 1
      shr   edx, cl
      or    eax, edx
    end;
      

  4.   

    代码3:function IntRol(v: LongWord): LongWord;
    var
      bits: Integer;
      tv: LongWord;
    begin
      tv := $80000000;
      bits := 31;
      while (bits >= 0) and (v and tv = 0) do
      begin
        Dec(bits);
        tv := tv shr 1;
      end;
      if bits > 0 then
        Result := ((v and not tv) shl 1) or (v shr bits)
      else
        Result := v;
    end;
      

  5.   

    前面代码1处理v=1时cl超范围,可改为:function IntRol(v: LongWord): LongWord;
    asm
      cmp   eax, 1
      jbe   @@1
      bsr   ecx, eax
      xor   ecx, 31
      xor   edx, edx
      inc   ecx
      shld  edx, eax, cl
      shl   eax, cl
      dec   ecx
      shr   eax, cl
      or    eax, edx
    @@1:
    end;代码3也可做些简化:function IntRol(v: LongWord): LongWord;
    var
      bits: Integer;
      tv: LongWord;
    begin
      if v > 1 then
      begin
        tv := $80000000;
        bits := 31;
        while v and tv = 0 do
        begin
          Dec(bits);
          tv := tv shr 1;
        end;
        Result := ((v and not tv) shl 1) or 1;
      end
      else
        Result := v;
    end;
      

  6.   

    procedure MoveLeft(var AStr: string);
    var
      c: char;
      ALen: integer;
    begin
      //循环左移字符串
      ALen := Length(AStr);
      c := AStr[1];
      Move(AStr[2], AStr[1], ALen - 1);
      AStr[ALen] := c;
    end;procedure MoveRight(var AStr: string);
    var
      c: char;
      ALen: integer;
    begin
      //循环右移字符串
      ALen := Length(AStr);
      c := AStr[ALen];
      move(AStr[1], AStr[2], ALen - 1);
      AStr[ALen] := c;
    end;