Thread overview
BigInt and >>>
Dec 19, 2014
Andre
Dec 19, 2014
bearophile
Dec 19, 2014
Andre
December 19, 2014
Hi,

I try to translate following javascript coding to D:

i = buffer[2] << 16;
i |= buffer[1] << 8;
i |= buffer[0];
i += buffer[3] << 24 >>> 0;

Buffer is:
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 111]

Expected result for i is: 4294967295
But in D the last statement returns -1, the previous 3 statements
returns the same values like in JavaScript.

I tried "long" and also like in the example "BigInt", both do not
work correctly. In the documentation it is also mentioned that
>>> is not supported for "BigInt"?

void main()
{
	import std.stdio;
	import std.bigInt;

	auto buffer = [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 111];
	BigInt i;
	
	i = buffer[2] << 16;
	i |= buffer[1] << 8;
	i |= buffer[0];
	i += buffer[3] << 24 >>> 0;
	writeln("i: ", i);
}

Do you have any idea how to translate the coding correctly?

Kind regards
André
December 19, 2014
Andre:

> Do you have any idea how to translate the coding correctly?

Try:

i += long(buffer[3]) << 24 >>> 0;

But I also suggest to add parentheses, to make the code less unreadable for humans.

Bye,
bearophile
December 19, 2014
Great! Thanks a lot.

Kind regards
André

On Friday, 19 December 2014 at 08:47:50 UTC, bearophile wrote:
> Andre:
>
>> Do you have any idea how to translate the coding correctly?
>
> Try:
>
> i += long(buffer[3]) << 24 >>> 0;
>
> But I also suggest to add parentheses, to make the code less unreadable for humans.
>
> Bye,
> bearophile