现在在做一个程序,需要把别人写的一段JAVASCRIPT加密算法转过来在位运算符的问题上出现了很多问题,虽然程序能执行,但是结果老是不对所以我觉得我转换出错了,所以就想知道javascript中的<< >> >>>这个几个位运算符到底应该如何转换成object pascal中的运算符呢??如a << b改怎么转换是 a shl b吗?a >> b 就是 a shr b吗?那a >>> b呢???希望有十分懂的人给出一个正确的解答!谢谢了

解决方案 »

  1.   

    >>>把二进制右移,左边补0
    >>把二进制右移,左边补和最高位相同的数字
      

  2.   

    >>>和shr一样,
    对于>>,需要在shr后,在左边补和最高位相同的数字
      

  3.   

    我想要具体的实现语句,操作数就是普通的int类型,有符号整数
      

  4.   

    var
    a:Integer;
    begin
    a shr 1;
    end;
      

  5.   

    楼上的是什么意思啊我需要<< >>和>>>对应的object pascl中的写发,完整的写法?
      

  6.   

    > > > 和shr一样
    对于> > :
    function ShrEx(iSource, i: integer):integer;
    var
      j: integer;
    begin
      result := 0;
      iSource := iSource shr i;
      if iSource >= 0 then
        result := iSource
      else 
      begin
        for j:=1 to i do
          result := result + (1 shl (32-j));    
        result := iSource and rusult;
      end;
    end;
      

  7.   

    javascript的>>和>>>好象是符号区分的
    全都对应object pascal的shr即可
    你用shr和shl即可.
      

  8.   

    result := iSource and result;
      

  9.   

    luxuewei5214 辛苦了,写了这么多代码,我再试试看,你那个32-j是什么意思?对应integer是用32,如果返回int64是不是还是32呢? 
      

  10.   

    >>>对应Shr
    <<<对应shl
    <<和>>在Delphi中没有操作符对应.可以写成函数.
    内嵌汇编版本的函数
      Function LShl(Value : Cardinal; Bit : Byte):Integer; register;
      asm
        mov cl, dl
        ROL eax, cl
      end;  Function LShr(Value : Cardinal; Bit : Byte):Integer; register;
      asm
        mov cl, dl
        ROR eax, cl
      end;
    Pascal版本的函数
      Function LShl(Value : Cardinal; Bit : Byte):Integer;
      begin
        Result := (Value shr (32 - Bit))or(Value shl Bit);
      end;
      Function LShr(Value : Cardinal; Bit : Byte):Integer;
      begin
        Result := (Value shl (32 - Bit))or(Value shr Bit);
      end;
      

  11.   

    Java对于操作符>>,<<,>>>的定义如下:
    >> op1 >> op2  右移op1 op2位  
    << op1 << op2 左移op1 op2位  
    >>> op1 >>> op2 右移op1 op2位 (无符号移位)  对于Delphi来说,这三个Java操作符对应Delphi的两个操作符shr和shl,
    而Delphi的shr的操作数如果是定义的有符号数,那么这时它对应java的>>
    即如果定义
      i:integer;//i是有符号数
      Delphi的 i shr 3 相当于Java的i>>3
    而如果Delphi的操作数如果是无符号数,那么这时shr对应Java的>>>

      i:byte;
      i shr 2相当于i>>>2
    另外:
      Java中没有<<<这个东西!