Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6688249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:22:58+00:00 2026-05-26T05:22:58+00:00

I have a fairly simple function in Delphi which takes a string and produces

  • 0

I have a fairly simple function in Delphi which takes a string and produces a hashed integer based on that string:

function TfrmMain.HashElf(const Buf;  BufSize : LongInt) : LongInt;
 var
 Bytes : TByteArray absolute Buf;
 I, X  : LongInt;
begin
  Result := 0;
  for I := 0 to BufSize - 1 do begin
    Result := (Result shl 4) + Bytes[I]; 
    X := Result and $F0000000;
    if (X <> 0) then  Result := Result xor (X shr 24);
    Result := Result and (not X);
  end;
end;

I’m converting it to PHP but the results are not the same. This is what i’ve got in PHP:

function HashElf($Buf, $BufSize){
  $Bytes = str_split($Buf);

  for ($i= 0; $i<$BufSize;$i++){
    $Result = ($Result << 4) + Ord($Bytes[$i]);

    $X = $Result & (0xF0000000);
    if ($X<>0){$Result = $Result ^ ($X>>24);}

    $Result = ($Result & (~ $X));
  }
  return $Result;
}

if you pass in the string teststring to the Delphi function you get 195831015 however PHP returns 72559895. I noticed the difference only becomes apparent after 7 characters. If the test string is just test the results are identical.

PHP seems to have some difficulty with shifting a negative integer to the right for example the folowing line:

 if ($X<>0){$Result = $Result ^ ($X>>24);}

changed to shift left $X<<24 produces the same values as Delphi for the variable X, but the results are still different.

Am i missing something really obvious here?

EDIT:
The output of the two functions are:

Delphi

  Char: t   Result: 116        X: 0
  Char: e   Result: 1957       X: 0
  Char: s   Result: 31427      X: 0
  Char: t   Result: 502948     X: 0
  Char: s   Result: 8047283    X: 0
  Char: t   Result: 128756644  X: 0
  Char: r   Result: 181058242  X: 1879048192
  Char: i   Result: 212577321  X: -1610612736
  Char: n   Result: 180011582  X: -1073741824
  Char: g   Result: 195831015  X: -1610612736

PHP

  Char: t   $Result: 116         $X: 0
  Char: e   $Result: 1957        $X: 0
  Char: s   $Result: 31427       $X: 0
  Char: t   $Result: 502948     $X: 0
  Char: s   $Result: 8047283    $X: 0
  Char: t   $Result: 128756644  $X: 0
  Char: r   $Result: 181058242  $X: 1879048192
  Char: i   $Result: 212577417  $X: -1610612736
  Char: n   $Result: 180013310  $X: -1073741824
  Char: g   $Result: 195858503  $X: -1610612736

So its not until character “i” that php starts to get off track with the calculations

EDIT2:

Added PHP function to do a logical right shift instead of arithmetic shift:

function lshiftright($var,$amt)
{
  $mask = 0x40000000;
  if($var < 0)
  {
    $var &= 0x7FFFFFFF;
    $mask = $mask >> ($amt-1);
    return ($var >> $amt) | $mask;
  }else{
    return ($var >> $amt);
  }
}

This now works! Also thanks Ignacio for the mask idea 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:22:59+00:00Added an answer on May 26, 2026 at 5:22 am

    Are you sure Delphi is right and PHP is wrong?

    Delphi’s shl and shr apparently can behave unpredictably with signed integers. See: http://www.merlyn.demon.co.uk/del-bits.htm#SAR. Dr. Stockton seems to imply there are two types of shift operations: arithmetic shift (keeping the sign) and logical shifts.

    The docs ( http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/devcommon/expressions_xml.html ) aren’t very clear on the effect of shl/shr on signed integers. They do however mention that shr/shl by one is only comparable to divions/multiplication by 2 for unsigned integers.

    I couldn’t find what Dr. Stockton (from the first link) calls the logical shift operations, but it would seem logical 🙂 to try changing the delphi implementation to use an unsigned 8-byte type (DWORD comes to mind) and see what effect that has.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Delphi application that I have written a fairly simple wrapper .exe
I have a fairly simple python loop that calls a few functions, and writes
I have a fairly simple const struct in some C code that simply holds
I have a fairly simple set of functionality for which I have multiple implementations,
I have a fairly simple PyQt application in which I'm placing instances of a
We have a fairly simple program that's used for creating backups. I'm attempting to
I have what I believe is a fairly simple application at the moment based
I have this fairly simple function to compute the mean of elements of a
I have a fairly simple program based largely on a simple bluetooth test client
I have a fairly simple web app built with Delphi (2009) Web Broker. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.