Bernard Burn
Bernard Burn

Reputation: 661

How to mod2^64 in long in Java?

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.)

Upvotes: 7

Views: 1021

Answers (3)

Philipp Wendler
Philipp Wendler

Reputation: 11433

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.

Upvotes: 2

Peter Lawrey
Peter Lawrey

Reputation: 533492

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?

Upvotes: 6

NPE
NPE

Reputation: 500257

BigInteger is one way to do it.

Upvotes: 3

Related Questions