www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - BigInt and >>>

reply "Andre" <andre s-e-a-p.de> writes:
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é
Dec 18 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Dec 19 2014
parent "Andre" <andre s-e-a-p.de> writes:
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
Dec 19 2014