A Small-C question, probably.
#1

Consider this code (I found somewhere years ago):

Код:
    stock udb_hash(buf[])
    {
        new length=strlen(buf);
        new s1 = 1;
        new s2 = 0;
        new n;
        for (n=0; n<length; n++)
        {
           s1 = (s1 + buf[n]) % 65521;
           s2 = (s2 + s1)     % 65521;
        }
        return (s2 << 16) + s1;
    }
Now this is my Java implementation:

Код:
	private double udb_hash(String pass)
	{
	    int length = pass.length();
	    int s1 = 1;
	    double s2 = 0;
	    double s3 = 0;
	    
	    for (int n = 0; n < length; n++)
	    {
	       s1 = (s1 + (int) pass.charAt(n)) % 65521;
	       s2 = (s2 + s1) % 65521;
	    }
	    
	    s3 = (Math.round(s2 << 16) + s1;  
	    return s3;
	}
Quora says it's impossible to use a bitwise operator on a double or floating-point variable; Pawn does it with ease. It works as long as I use Math.round to make the result an integer, but that doesn't work properly for hashing. Why is this?

EDIT: I don't know why I didn't realize that since Pawn uses dynamic types it converts the floating-point to a string (array of chars) and THEN shifts. My issue is solved.
Reply


Messages In This Thread
A Small-C question, probably. - by [eLg]elite - 19.02.2019, 02:52
Re: A Small-C question, probably. - by KingHual - 19.02.2019, 09:28
Re: A Small-C question, probably. - by [eLg]elite - 19.02.2019, 14:34
Re: A Small-C question, probably. - by chickin - 19.02.2019, 14:39
Re: A Small-C question, probably. - by FedeA - 19.02.2019, 21:09

Forum Jump:


Users browsing this thread: 1 Guest(s)