Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mod2^64 in long in Java?

Tags:

java

modulo

I'm implementing a Skein hash function in Java, and I have a problem with a part where some additions where modulo 2^64. As I we know, long in java has max value = 2^63-1. So my problem is, how to implement this modulo operation. (All operations in Skein are on 64bit words.)

like image 814
Bernard Burn Avatar asked Jan 21 '12 14:01

Bernard Burn


3 Answers

long in Java is 64-bit so all operations are mod 2^64 already. You don't have to do anything extra to make that happen.

Is the problem that you don't know how to handle signed values?

Is this something you want or is it something you are trying to avoid?

like image 123
Peter Lawrey Avatar answered Nov 04 '22 17:11

Peter Lawrey


BigInteger is one way to do it.

like image 45
NPE Avatar answered Nov 04 '22 17:11

NPE


In addition to Peter's answer, I want to suggest taking a look at the great Guava library. It has a class UnsignedLongs which offers several utility functions for working with longs treating them as unsigned. This might be helpful for you.

like image 24
Philipp Wendler Avatar answered Nov 04 '22 16:11

Philipp Wendler