Recent

Author Topic: Shr function - pascal and php  (Read 23209 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Shr function - pascal and php
« on: March 25, 2010, 02:44:25 pm »
Question! But the SHR has problems?

Why I compared this function:

function PKV_GetKeyByte(const Seed : Int64; a, b, c : Byte) : Byte;
var
   risultato: byte;
begin
  a := a mod 25;
  b := b mod 3;
 if a mod 2 = 0 then
    risultato := ((Seed shr a) and $000000FF) xor ((Seed shr b) or c)
  else
    risultato := ((Seed shr a) and $000000FF) xor ((Seed shr b) and c);
    PKV_GetKeyByte:=risultato;
end;


I had to write this other function in php, but the results are different

   private function PKV_GetKeyByte($Seed,$a,$b,$c)
   {
      $a = $a % 25;
        $b = $b % 3;
       if(  $a % 2 == 0 )
       {
          $result = ( ( $Seed >> $a ) && 0x000000FF ) xor ( ( $Seed >> $b ) || $c );
       }else
       {
          $result = ( ( $Seed >> $a ) && 0x000000FF ) xor ( ( $Seed >> $b ) && $c );
       }       
      return $result;
   }

Can someone help me? I think it's a problem for me because of Pascal syntax of PHP, it seems correct!
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11818
  • FPC developer.
Re: Shr function - pascal and php
« Reply #1 on: March 25, 2010, 03:58:37 pm »
What is exactly different?


xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: Shr function - pascal and php
« Reply #2 on: March 25, 2010, 04:11:50 pm »
  KeyBytes[0] := PKV_GetKeyByte(Seed, 24, 3, 200);
  KeyBytes[1] := PKV_GetKeyByte(Seed, 10, 0, 56);
  KeyBytes[2] := PKV_GetKeyByte(Seed, 1, 2, 91);
  KeyBytes[3] := PKV_GetKeyByte(Seed, 7, 1, 100);

Try running the following values, Seed = 1001
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2614
Re: Shr function - pascal and php
« Reply #3 on: March 25, 2010, 04:53:43 pm »
What values do you get and what do you expect ?

Not everyone is able to run the php code
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: Shr function - pascal and php
« Reply #4 on: March 25, 2010, 04:59:23 pm »
Pascal:
 
 a           b            Seed shr a
 24         0                     0
 10         0                     0
 1           2                   244
 7           1                     7


Php:

 a           b            $Seed >> $a
 24         0                     0
 10         0                     0
 1           2                   500
 7           1                     7
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11818
  • FPC developer.
Re: Shr function - pascal and php
« Reply #5 on: March 25, 2010, 05:38:22 pm »
500-244=256

The typing of your intermediate result is wrong. A byte can only contain 0..255.

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2614
Re: Shr function - pascal and php
« Reply #6 on: March 25, 2010, 05:49:28 pm »
((Seed shr a) and $000000FF) ->

1001 shr 1 -> 500

500 and $FF -> 244

I wonder why only the part with a is anded with $FF and not b and C

thats why I asked what values you do expect for:

  PKV_GetKeyByte(1001, 24, 3, 200)
  PKV_GetKeyByte(1001, 10, 0, 56)
  PKV_GetKeyByte(1001, 1, 2, 91)
  PKV_GetKeyByte(1001, 7, 1, 100)

« Last Edit: March 25, 2010, 05:51:13 pm by Marc »
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

dfeher

  • New Member
  • *
  • Posts: 19
Re: Shr function - pascal and php
« Reply #7 on: March 25, 2010, 07:19:10 pm »
Hi!

At first sight, I think the Pascal and PHP codes returns different results because of using non-bitwise operators in the PHP code:

Code: [Select]
      if(  $a % 2 == 0 )
       {
          $result = ( ( $Seed >> $a ) && 0x000000FF ) xor ( ( $Seed >> $b ) || $c );
       }else
       {
          $result = ( ( $Seed >> $a ) && 0x000000FF ) xor ( ( $Seed >> $b ) && $c );
       }       

&& is logical and, || is logical or.

I think you have to use & and | instead, and ^ instead of xor.

I hope this helps!

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: Shr function - pascal and php
« Reply #8 on: March 26, 2010, 07:40:33 am »
1:  PKV_GetKeyByte(1001, 24, 3, 200)
2:  PKV_GetKeyByte(1001, 10, 0, 56)
3:  PKV_GetKeyByte(1001, 1, 2, 91)
4:  PKV_GetKeyByte(1001, 7, 1, 100)

Result php

1: 1001
2: 1017
3: 174
4: 99

Result pascal

1: 233
2: 249
3: 174
4: c

but I changed the code as said dfeher
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2614
Re: Shr function - pascal and php
« Reply #9 on: March 26, 2010, 10:20:18 am »
Good that you changed :)

Resultvalues of 1001 or 1017 will never fit in a byte.
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: Shr function - pascal and php
« Reply #10 on: March 26, 2010, 10:25:25 am »
I know but I also do not understand why! But I need to translate it into PHP to interact with a site and program in Pascal! Ugh someone can help me?
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Leledumbo

  • Hero Member
  • *****
  • Posts: 8772
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Shr function - pascal and php
« Reply #11 on: March 26, 2010, 10:34:59 am »
Perhaps there's an overflow? Try turning on overflow check.

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: Shr function - pascal and php
« Reply #12 on: March 26, 2010, 10:38:40 am »
How it works?
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2614
Re: Shr function - pascal and php
« Reply #13 on: March 26, 2010, 10:54:43 am »
it has nothing to do with overflow (at least turning of a check wont change it)

A byte can hold values from 0..255, so if you need larger values, use a different type for your result. This is one of the problems with untyped languages like php. You don't know anything about the values.

So now look the "formula": ((Seed shr a) and $000000FF) xor ((Seed shr b) or c)

Seed is a Int64.
(Seed shr a) and $FF will fit in a byte (due to the and $FF)
(Seed shr b) can be anything so the max is still a Int64, especially if you look ar what values b can be (0,1,2)
c is a byte.

So in the end we have: Byte xor (Int64 or Byte)
This means that your result can have a range of Int64. Why did you choose a Byte ?
Unless you know your input values, atleast the following should work (but I won't call it GetKeyByte, since it isnt):
Code: Pascal  [Select][+][-]
  1. function PKV_GetKeyByte(const Seed : Int64; a, b, c : Byte) : Int64;
  2. begin
  3.   a := a mod 25;
  4.   b := b mod 3;
  5.  if a mod 2 = 0 then
  6.     Result := ((Seed shr a) and $000000FF) xor ((Seed shr b) or c)
  7.   else
  8.     Result := ((Seed shr a) and $000000FF) xor ((Seed shr b) and c);
  9. end;
  10.  
« Last Edit: March 26, 2010, 10:57:37 am by Marc »
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: Shr function - pascal and php
« Reply #14 on: March 26, 2010, 11:05:21 am »
Then we assume that in pascals ottento the correct result, how do I get the same in php?
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

 

TinyPortal © 2005-2018