Better HN
topnewbestaskshowjobs
Log inSign up

pzemtsov

2 karma · joined Mar 24, 2017 (9y ago) · 3 submissions

Submissions

comment 7y ago · link · parent

This is all actually pretty safe.

Firstly, Java, unlike C, makes full commitments to the bit-to-bit results of all arithmetic calculations. C only does that for the unsigned calculations, giving a compiler some freedom for the signed case. Java postulates that everything is calculated in binary 2's complememt code. No other arithmetic engines are allowed.

Secondly, within this engine, everything is done modulo 2^32, even despite the fact that Java ints are signed. At least, this is so as long as you avoid division. Multiplication of the unsigned numbers produces bit-to-bit the same result as that of unsigned ones. For instance, let's multiply -1 by -1 (FFFFFFFF by FFFFFFFF). It, obviously, produces 1. Now, let's go unsigned: FFFFFFFF times FFFFFFFF is FFFFFFFE00000001, or, in the last 32 bits, 1.

So, if in Java you want to perform any computations modulo 2^32, go for it. Just avoid division.

comment 9y ago · link · parent

I've added an update on the nanoTime() to the article

3.

Statistical variables in Java: not quite legal but very fast(pzemtsov.github.io ↗)

3 points · by pzemtsov · 9y ago · discuss