Magic number in Boolean

H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message

Hi,

I browsed through the source of java.lang.Boolean, and encountered the
following:

public int hashCode() {
return value ? 1231 : 1237;
}

Now I was just wondering: does anybody have any idea where these two
magic numbers come from?

H.
--
Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFEEXIxe+7xMGD3itQRApW3AJ91WgDeR+gqRhAw6OoSInMeHomS3gCfQRz5
wztqk+leZvUzdXiFG18/23A=
=MWwG
-----END PGP SIGNATURE-----
 
A

Alex Hunsley

Hendrik said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message

Hi,

I browsed through the source of java.lang.Boolean, and encountered the
following:

public int hashCode() {
return value ? 1231 : 1237;
}

Now I was just wondering: does anybody have any idea where these two
magic numbers come from?

For a start, they're both prime. You'll often find prime numbers used in
hashing functions - primes behave more favourably in hashing functions
(and sometimes guarantee some behaviours).
As to why they chose those two primes in particular, I'm not sure.
 
R

Roedy Green

public int hashCode() {
return value ? 1231 : 1237;
}

do these numbers look familiar:

1193 1201 1213 1217 1223 1229 1231 1237 1249 1259 1277 1279 1283
 
?

=?ISO-8859-1?Q?Jan_Thom=E4?=

Roedy said:
do these numbers look familiar:

1193 1201 1213 1217 1223 1229 1231 1237 1249 1259 1277 1279 1283

Except for being all prime, no. Mind to enlighten us?

Jan
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message

Roedy Green schreef:
do these numbers look familiar:

1193 1201 1213 1217 1223 1229 1231 1237 1249 1259 1277 1279 1283

Ok, but then why not 1 and 2, they are also both prime. Of course I see
the use of taking some bigger number, but still the question remains:
why some primes around 1200? Because accidentally the implementor knew
them, then?

Well, that is a satisfying answer, actually :)

H.
--
Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFEEaBke+7xMGD3itQRAsqqAJ9byE7oGFkF7eXnWpphO0buEVyxiACfbYcC
n4G5/b8bek45HLDd29NQVFM=
=ZimM
-----END PGP SIGNATURE-----
 
O

Oliver Wong

Hendrik Maryns said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message

Roedy Green schreef:

Ok, but then why not 1 and 2, they are also both prime. Of course I see
the use of taking some bigger number, but still the question remains:
why some primes around 1200? Because accidentally the implementor knew
them, then?

Well, that is a satisfying answer, actually :)

Ideally, you don't want hash collisions, so you don't want to choose the
same prime number as anyone else. "1" and "2" seem too "obvious", so they
might be avoided in favor of more obscure ones.

- Oliver
 
R

Roedy Green

Ok, but then why not 1 and 2, they are also both prime. Of course I see
the use of taking some bigger number, but still the question remains:
why some primes around 1200? Because accidentally the implementor knew
them, then?

Consider what happens when you create a composite hashCode either by
multiplication, addition, xor, shift/or etc .

My guess is those numbers help the boolean effect from getting lost.
 
E

Eric Sosman

Alex Hunsley wrote On 03/10/06 08:03,:
For a start, they're both prime. You'll often find prime numbers used in
hashing functions - primes behave more favourably in hashing functions
(and sometimes guarantee some behaviours).
As to why they chose those two primes in particular, I'm not sure.

Programmers are a superstitious lot. Certain kinds
of numbers seem to them to have magical properties, and
since the magical numbers aren't the layman's seven or
thirteen, programmers think they're being technologically
savvy when they mumble their numerological incantations.

Powers of two, of course, are the most magical of all:
how often have you seen 128 used as the size of a buffer
for an input line, or 1024 for some other J. Random Array?
I've seen programmers carefully use power-of-two sizes
with C's malloc() "for efficiency," failing to realize
that for some implementations this is absolutely the least
efficient thing they could possibly do!

After powers of two (a long way after), programmers
attribute mystical powers to the prime numbers. Somewhere
in the dim racial memory they recall there was something
about hash tables and prime numbers. Next think you know,
"Something I can't quite recall, but there were primes in
it" metamorphoses into "Every number in any way connected
with a hash table should be prime, and don't look at the
full Moon over your left shoulder lest you die on the spot."

Phooey, blooey, and double-screwy! If anyone can come
up with a reason -- a seriously researched reason, none of
these "My Gypsy grandmother told me" fairy tales -- to favor
prime numbers as hashCode() values, I will eat all the
electrons expended on this diatribe, one by one.

Oh, and for the record: "... 1 and 2, they are also
both prime" is only fifty percent correct.
 
R

Raymond DeCampo

Hendrik said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message

Roedy Green schreef:



Ok, but then why not 1 and 2, they are also both prime. Of course I see
the use of taking some bigger number, but still the question remains:
why some primes around 1200? Because accidentally the implementor knew
them, then?

I can't help but point out that 1 is not technically a prime number.
Not that it diminishes your point in any way.

Ray
 
L

Luc The Perverse

Raymond DeCampo said:
I can't help but point out that 1 is not technically a prime number. Not
that it diminishes your point in any way.

I've heard -1 considered a prime of integers (not of natural numbers of
course)
 
C

Chris Uppal

Eric said:
After powers of two (a long way after), programmers
attribute mystical powers to the prime numbers. Somewhere
in the dim racial memory they recall there was something
about hash tables and prime numbers. Next think you know,
"Something I can't quite recall, but there were primes in
it" metamorphoses into "Every number in any way connected
with a hash table should be prime, and don't look at the
full Moon over your left shoulder lest you die on the spot."

Phooey, blooey, and double-screwy! If anyone can come
up with a reason -- a seriously researched reason, none of
these "My Gypsy grandmother told me" fairy tales -- to favor
prime numbers as hashCode() values, I will eat all the
electrons expended on this diatribe, one by one.

<grin>

You should be happy with the chosen values then. Unless I've messed up the
calculation, the hash scrambler in the default HashMap maps them:

1237 --> 10794756
1231 --> 10746253

neither of which are prime, and nor are they even co-prime ;-)

-- chris
 
O

Oliver Wong

Luc The Perverse said:
I've heard -1 considered a prime of integers (not of natural numbers of
course)

Depends on your definition of prime. If we ignore, for now, negative
numbers, here are the two most common definition for "prime number" I've
heard:

(A) A number which is only divisible by 1 and itself.
(B) A number with exactly 2 distinct factors.

Under definition (A), 1 is prime. Under definition (B), 1 is not.

If you allow for negative prime numbers, you have to adjust the above
definitions accordingly. (E.g. only divisible by itself, its negation, 1
and -1, or with exactly 4 distinct factors).

- Oliver
 
T

Thomas Hawtin

Luc said:
I've heard -1 considered a prime of integers (not of natural numbers of
course)

-1 and 1 are units in the (rational) integers. 0 is the annihilator. The
rest are primes or composites.

For Gaussian integers, the units are 1, -1, i and -i. (I guess you could
have a two-dimensional hashtable, but I don't know why you'd bother.)

For rationals, everything except 0 is a unit.

(Disclaimer: It's over a decade since I completed my degree, and it
wasn't a very good one.)

Tom Hawtin
 
T

Thomas Hawtin

No, I think you like electrons, Briar Rabbit.
You should be happy with the chosen values then. Unless I've messed up the
calculation, the hash scrambler in the default HashMap maps them:

1237 --> 10794756
1231 --> 10746253

neither of which are prime, and nor are they even co-prime ;-)

But there are no common factors between theses numbers and any of the
possible sizes used by HashMaps (we are talking the 1.4.2 implementation
here), unless 7 has suddenly become a power of two.

Happily, we only get a collision with a HashMap size of one and a
whopping great load factor.

Tom Hawtin
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message

Raymond DeCampo schreef:
I can't help but point out that 1 is not technically a prime number. Not
that it diminishes your point in any way.

Ah, you?re right, in the strict mathematical way, but even though I am a
mathematician, I still like to think it is. (/me knocks himself on the
head).

H.
--
Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFEFUrge+7xMGD3itQRArPhAJ4lg1OnGOw6WfsgpERvs+F6W3XWHgCfcetk
n+fu0UKRNg14ouJM5WeLqpc=
=qGQD
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top