There are some differences between (Objective-)C and Java, but at a guess you may have identified the wrong problem. Java does not have unsigned integer types. However it does have bitwise operators which treat integers as just strings of bits, and later versions of Java do have unsigned operators which when applied to integers operate on the underlying bit patterns as though they represented unsigned values.
The expression crc & 1
will yield a result of 0
or 1
depending on whether crc
was even or odd.
The expression -(crc & 1)
therefore evaluates to 0
or -1
. The bit pattern of 0
is all zeros and for -1
it is all ones - the latter is true in (Objective-)C regardless of the underlying representing of integers as 1's or 2's complement, see Is it safe to use -1 to set all bits to true? for an explanation.
The expression 0xedb88320ul & (-(crc & 1))
therefore evaluates to 0
or 0xedb88320
.
The expression crc >> 1
is the logical, or unsigned, shift right in (Objective-)C as src
is defined as unsigned (right shifts for signed integers are "implementation defined" in (Objective-)C).
The expression x ^ 0
for any x
evaluates to x
.
Put that all together and your whole expression (crc >> 1) ^ (0xedb88320ul & (-(crc & 1)))
conditionally xors crc >> 1
with 0xedb88320
base don whether crc
is odd or even.
Now you just need to reproduce the same behaviour in Java.
Java uses 32-bit 2's complement integers for int
(JLS: Primitive Types and Values) and provides bitwise operators which treat them as just bit-strings.
The hexadecimal constant comes just 0xedb88320
and has int
(32-bit) type.
The expression -(crc & 1)
therefore evaluates as in (Objective-)C.
However the expression crc >> 1
will not evaluate the same as in (Objective-)C. In Java the >>
is an arithmetic shift right, which replicates the sign-bit during the shift. This choice stems from Java using signed types. Fortunately Java also provides the >>>
operator to perform a logical, or unsigned, right-shift.
So my guess is you may have identified the wrong problem in your conversion and you need to use the >>>
operator.
HTH