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

0

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
c#
php
bit-shift
asked on Stack Overflow Mar 31, 2021 by danishqamar

1 Answer

1

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.

answered on Stack Overflow Mar 31, 2021 by Kapitany

User contributions licensed under CC BY-SA 3.0