java - Java8 unsigned arithmetic -


java 8 reported have library support unsigned integers. however, there seem no articles explaining how use , how possible.

some functions integer.compareunsigned easy enough find , seem 1 expect. however, fail write simple loop loops on powers of 2 within range of unsigned long.

int = 0; for(long l=1; (long.compareunsigned(l, long.max_value*2) < 0) && i<100; l+=l) {     system.out.println(l);     i++; } 

produces output

1 2 4 8 ... 1152921504606846976 2305843009213693952 4611686018427387904 -9223372036854775808 0 0 0 ... 0 

am missing or external libraries still required simple task?

if you're referring to

(long.compareunsigned(l, long.max_value*2) < 0) 

l reaches

-9223372036854775808 

unsigned

9223372036854775808 

and

long.max_value*2 

is

18446744073709551614 

so l smaller long.max_value*2 in unsigned world.

assuming you're asking 0's

0 0 0 ... 0 

the problem (if see way) that, long (other numerical primitives), first bit sign bit.

so

10000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 

is

-9223372036854775808 

when

-9223372036854775808 + -9223372036854775808 

you underflow (overflow?) since

    10000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 +   10000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 

is

00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 

which 0. on later loop iterations, 0 + 0 remains 0.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -