Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UUID.randomUUID().getLeastSignificantBits() always returns negative values?

Tags:

java

uuid

I need to generate a unique long value, so I decided to use UUID:

UUID.randomUUID().getLeastSignificantBits();

One strange thing I noticed was that UUID.randomUUID().getLeastSignificantBits() always returned negative values. I am confused. Am I missing something?

like image 961
Kai Avatar asked Nov 04 '14 23:11

Kai


1 Answers

Wikipedia says:

Version 4 UUIDs use a scheme relying only on random numbers. This algorithm sets the version number (4 bits) as well as two reserved bits. All other bits (the remaining 122 bits) are set using a random or pseudorandom data source.

Version 4 UUIDs have the form xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx where x is any hexadecimal digit and y is one of 8, 9, A, or B

So the first bit of the least significant half will always be a 1, making it a negative number.

like image 118
Thilo Avatar answered Oct 16 '22 23:10

Thilo