简体   繁体   中英

ulong, bitmask and bit shift in PHP from C#

Ok so my problem is that I've to " process " a big integer from an endpoint response. This is a hash and I've to do some bit mask and bit shifts in order to view this data (it's a version: major, minor, build, etc) in a human readable way.

In C# I've easily written few lines to do this processing :

/// Endpoint data
ulong data = 567454918934921216;

ulong major = (version & 0xFFFF000000000000L) >> 48;
ulong minor = (version & 0x0000FFFF00000000L) >> 32;
ulong build = (version & 0x00000000FFFF0000L) >> 16;
ulong revision = version & 0x000000000000FFFFL;

string human = $"{major}.{minor}.{build}.{revision}";
Console.WriteLine(human);

/// Returns '2016.318.1322.0'

However this should be part of a backend task running on a server , so I've to write it in PHP . The problem is that PHP handle that big integer ( data ) as float cutting a significant part of it (and I need every single bit for doing that processing).

So the question is: how can I "port" that C# code in normal PHP?

Important notice: I cannot access PHP installation or modifiy server architecture/configuration. So I can't shift to 64bit to handle bigger integer or install plugins.

PHP int size is platform-dependent. You need 64-bit hardware AND the 64-bit version of PHP to get native 64-bit integer. You can get 64-bit integer from 32-bit integers using bitwise operators. For example: to build 0xfffffffffffffffe you can use (0xffffffff << 32) | 0xfffffffe (0xffffffff << 32) | 0xfffffffe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM