How to convert this function into java?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • m.j

    How to convert this function into java?

    Hi,

    How to convert this pascal(delphi) function into java?
    The function is used in a algorithm.

    The trouble is that java don't have any unsigned 32bit type.

    What the trick, when you cannot do bit shifting probaly,
    because only signed types are aval.?


    B12Ar is a array of 12 bytes(0..11), and can be anything.
    "P" can be "0" or "8".
    "k" can be "10" or "31".
    -----------
    procedure Cyc32(var Buf: B12Ar;P: byte;k: integer);
    var
    i : integer;
    x,y : Longword;

    begin
    x := Buf[P];
    x := x shl 8;
    x := x or Buf[P + 1];
    x := x shl 8;
    x := x or Buf[P + 2];
    x := x shl 8;
    x := x or Buf[P + 3];

    for i := 1 to k do
    begin
    // Cyclical Rotate 32 bits word right k times
    y := x and 1; // Extract Carry
    y := y shl 31; // Move Carry to Msb
    x := x shr 1; // Do Shift
    x := x or y; // Add Carry
    end;

    Buf[P + 3] := x;
    x := x shr 8;
    Buf[P + 2] := x;
    x := x shr 8;
    Buf[P + 1] := x;
    x := x shr 8;
    Buf[P] := x;
    end;
    -----------


    --
    Best regrads
    M.J


  • Thomas A. Li

    #2
    Re: How to convert this function into java?

    In Java there are three shift operators:
    <<[color=blue][color=green]
    >>[color=darkred]
    >>> use this one[/color][/color][/color]

    Make sure you read a good Java book. It pays.

    "m.j" <schoolback@hot mail.com> wrote in message
    news:3fd67201$0 $169$edfadb0f@d text02.news.tel e.dk...[color=blue]
    > Hi,
    >
    > How to convert this pascal(delphi) function into java?
    > The function is used in a algorithm.
    >
    > The trouble is that java don't have any unsigned 32bit type.
    >
    > What the trick, when you cannot do bit shifting probaly,
    > because only signed types are aval.?
    >
    >
    > B12Ar is a array of 12 bytes(0..11), and can be anything.
    > "P" can be "0" or "8".
    > "k" can be "10" or "31".
    > -----------
    > procedure Cyc32(var Buf: B12Ar;P: byte;k: integer);
    > var
    > i : integer;
    > x,y : Longword;
    >
    > begin
    > x := Buf[P];
    > x := x shl 8;
    > x := x or Buf[P + 1];
    > x := x shl 8;
    > x := x or Buf[P + 2];
    > x := x shl 8;
    > x := x or Buf[P + 3];
    >
    > for i := 1 to k do
    > begin
    > // Cyclical Rotate 32 bits word right k times
    > y := x and 1; // Extract Carry
    > y := y shl 31; // Move Carry to Msb
    > x := x shr 1; // Do Shift
    > x := x or y; // Add Carry
    > end;
    >
    > Buf[P + 3] := x;
    > x := x shr 8;
    > Buf[P + 2] := x;
    > x := x shr 8;
    > Buf[P + 1] := x;
    > x := x shr 8;
    > Buf[P] := x;
    > end;
    > -----------
    >
    >
    > --
    > Best regrads
    > M.J
    >
    >[/color]


    Comment

    • m.j

      #3
      Re: How to convert this function into java?

      Hi,
      [color=blue]
      > In Java there are three shift operators:
      > <<[color=green][color=darkred]
      > >>
      > >>> use this one[/color][/color][/color]
      Hmm don't seem like there is any diff between >> and >>> in my JVM.
      (Still have to AND result, to set prior bits to 0)??

      But thanks anyway,
      i got it working now.
      Added some AND 0xff and stuff like that also, and then it suddenly worked.
      [color=blue]
      > Make sure you read a good Java book. It pays.[/color]
      I will do, do you know any good internet links?

      Thanks!

      --
      Best regrads
      M.J


      Comment

      Working...