开发者

PHP: Combine Two 16-bit Integers into a 32-bit integer

开发者 https://www.devze.com 2023-01-03 17:15 出处:网络
I am trying to combine two integers in my application. By combine I mean stick one byte stream at the end of the other, not concatenate the strings.

I am trying to combine two integers in my application. By combine I mean stick one byte stream at the end of the other, not concatenate the strings.

The two integers are passed fro开发者_如何转开发m hardware that can't pass a 32 bit value directly, but passes two consecutive 16-bit values separately.

Thanks,


$a = ($a << 16) | $b;


I have no idea of the syntax in PHP, but I bet it supports bitwise operators.

You can shift the first 16 bit integer to the left and add it to the second 16 bit integer:

first integer:
                0000000000000001
second integer:
                0000000000000010
shift first to the left by, say, 16:

first integer:
00000000000000010000000000000000
second integer:
                0000000000000010
add them:
00000000000000010000000000000010
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号