RSS

  • YouTube
  • LinkedIn
  • Google

Converting xor, shr and shl from Delphi to PHP

Share in your social media
  •  ;
  •  ;
  •  ;
  •  ;

I'm migrating a software made in Delphi to PHP and I came across a problem in a fucking Blessed encryption function.

I suffered a bit trying to rewrite the function does the complexity of the code and with the differences in the results.

The function makes use of operators XOR and SHR in Delphi version. The SHR was relatively easy thanks to experience with bitwise operators and how Delphi documentation says that the operator pulls off bits to the right

of an integer (How to understand an abbreviation of SHIFT Rlight and the inverse is the operator SHL ; SHIFT LEFT) I ran some tests using the operator >;>; and hit him first.

Already the XOR operator was more complicated. PHP has this operator and I didn't work to make sure if both do the same thing. Only when I did some tests using different integers is I could realize that they do not give the same results. In Delphi it always returned a number while in PHP he returned 1 or 0 (actually true or false).

Only when I read the documentation of both languages I realized that are different. In Delphi XOR is a exclusive comparison operation (OR exclusive) which returns true or false Or a bitwise operation of two arguments. PHP is just a XOR operator to OR exclusive. He doesn't make the adjustments in bit operation. The equivalent in this case is the operator ^ (caret).

Another learning. Hope that helps someone else someday.

Follows some useful links.

http://php.net/manual/pt_BR/language.operators.bitwise.php

http://www.delphibasics.co.uk/RTL.asp?Name=xor


Share in your social media
  •  ;
  •  ;
  •  ;
  •  ;

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.