# signed / unsigned integer problem

I have a function in C++:

unsigned int x, y, z;

y = seed;

for (x = 0; *password; password++) {
x = (x & 0xffffff00) | *password;
y ^= x;
y += x;
x <<= 8;
y ^= x;
x <<= 8;
y -= x;
x <<= 8;
y ^= x;

z = y & 0x1f;
y = (y << z) | (y >> (32 - z));
}
return y;
}

... and i have converted it to PHP:

\$y = \$seed;
\$x = 0;
for (\$nr = 0, \$length = strlen(\$password); \$nr < \$length; \$nr++) {
\$x = (\$x & 0xFFFFFF00) | ord(\$password[\$nr]);
\$y ^= \$x;
\$y += \$x;
\$x <<= 8;
\$y ^= \$x;
\$x <<= 8;
\$y -= \$x;
\$x <<= 8;
\$y ^= \$x;

\$z = \$y & 0x1F;
\$y = (\$y << \$z) | (\$y >> (32 - \$z));
}
return \$y;
}

The problem is that it doesn't realy seem to return the same value.
I think the problem is that there are variables in the C++ code marked as unsigned int, which i can not do in PHP.
Can someone help me converting that so it will work correctly?

Example:
Calling code from C++:
gg_login_hash("dupa", 128) gives 1453413288 in a result.
LVL 5
###### Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CTOCommented:

Hmm, /me thinks.
From a quick debug looks like the \$y >> (32 - \$z) is performing an aritmetic shift right rather than a logical shift right, as it does (and should do) in C.
This made me think a little so i decided to check the PHP manual for the bitwise operators and in fact a comment points out that all the shift operations are signed (thus aritmetical). Very bad!

So here is a hackish workaround to clean the sign and complete the shifting:
...
\$z = \$y & 0x1F;
\$y1 = (\$y << \$z);
\$y2 = 0;
if (\$z < 32) {
\$y2 = \$y >> 1;
\$y2 &= 0x7fffffff;
\$y2 = \$y2 >> (31 - \$z);
}
\$y = \$y1 | \$y2;
...

I'm the best PHP coder ;-)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.