danishqamar
danishqamar

Reputation: 1

Different Value of left and right shift in PHP and C#

Below is the left shift code in c# and php7.

Why and how to match PHP output with C# output?

Thanks in advance.

C#

    var ret = 0xFFFFFFFF;
    var a=ret << 8;
    Console.WriteLine(a);

Output

        4294967040

where as

PHP

    $ret = 0xFFFFFFFF;
    $a=$ret << 8;
    echo $a;

Output

       1099511627520

Upvotes: 0

Views: 80

Answers (1)

Viktor T&#246;r&#246;k
Viktor T&#246;r&#246;k

Reputation: 1319

Don't use the var keyword, use explicitly long instead of it:

long ret = 0xFFFFFFFF;
long a = ret << 8;
Console.WriteLine(a);

The var will use a 32 bit integer not a 64 bit integer.

Upvotes: 1

Related Questions