By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,554 Members | 1,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,554 IT Pros & Developers. It's quick & easy.

How to convert this function into java?

P: n/a
m.j
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
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
In Java there are three shift operators:
<<
use this one

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

"m.j" <sc********@hotmail.com> wrote in message
news:3f*********************@dtext02.news.tele.dk. .. 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

Jul 17 '05 #2

P: n/a
m.j
Hi,
In Java there are three shift operators:
<<

use this one
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.
Make sure you read a good Java book. It pays.

I will do, do you know any good internet links?

Thanks!

--
Best regrads
M.J
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.