Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

negative hash code

Thread view: 
Carramba - 21 Jan 2006 10:00 GMT
hello

Iam wondering if it's ok to get negative values from hash code?
it seems strange to me, since hashcode should reffer to place in memory,  
or have I got it wrong?

public class Test {

    public static void main(String[] args) {
        String z = "string";
        System.out.println(z);
        System.out.println("1: "+z.hashCode());

    }
    public int hashCode() {
        int hash = 1;
        hash = hash*17+this.hashCode();
        return hash;
    }//@-->---
}//@-->---

Output:
string
1: -891985903

Signature

Thanx in advance
________________________

Torkel Franzen - 21 Jan 2006 10:42 GMT
> Iam wondering if it's ok to get negative values from hash code?

 Sure. Before using the hash code we just make sure to set the sign bit
to 0.
Carramba - 21 Jan 2006 10:48 GMT
But by doing so, it results in pitfall? since you can have to diffetern  
hashcodes with differens in sign?
would't it make more sence to genere hachcode witch would by nonnegative  
directly? instead of shifting bits?

>> Iam wondering if it's ok to get negative values from hash code?
>
>   Sure. Before using the hash code we just make sure to set the sign bit
> to 0.

Signature

Thanx in advance
________________________

Roedy Green - 21 Jan 2006 10:56 GMT
>But by doing so, it results in pitfall? since you can have to diffetern  
>hashcodes with differens in sign?
>would't it make more sence to genere hachcode witch would by nonnegative  
>directly? instead of shifting bits?

no. You don't use the hashcode directly to address anything anyway.
you always collapse it further.  Why waste a bit. Sometimes you might
use it as an unsigned quantity.  

All the techniques for generating them will produce essentially random
numbers which will include negatives. There is no point on insisting
they be negative.  The produces would have to trim the sign and the
collection would have to check it was trimmed.  Better to just let the
collection trim the sign it if needs to.

What you often want in the remainder after division by some prime or
power of two. See http://mindprod.com/jgloss/modulus.html
The sign trimming comes out in the wash.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 21 Jan 2006 10:53 GMT
>Iam wondering if it's ok to get negative values from hash code?
>it seems strange to me, since hashcode should reffer to place in memory,  
>or have I got it wrong?

negative is fine. It is just a digest, not a slot in ram. See
http://mindprod.com/jgloss/hashcode.html
and http://mindprod.com/hashtable.html
for how it works.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

IchBin - 21 Jan 2006 21:33 GMT
>> Iam wondering if it's ok to get negative values from hash code?
>> it seems strange to me, since hashcode should reffer to place in memory,  
[quoted text clipped - 4 lines]
> and http://mindprod.com/hashtable.html
> for how it works.
http://mindprod.com/jgloss/hashtable.html

Signature

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)

Carramba - 21 Jan 2006 11:17 GMT
who complex should one writte hashcode alghorit? is it enought like in my  
exemple?
Hendrik Maryns - 22 Jan 2006 22:15 GMT
Carramba uitte de volgende tekst op 01/21/2006 12:17 PM:
> who complex should one writte hashcode alghorit? is it enought like in
> my exemple?

You hashcode function loops.  What do you think it will output?

H.

- --
Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org
Thomas Hawtin - 21 Jan 2006 11:50 GMT
> it seems strange to me, since hashcode should reffer to place in memory,
> or have I got it wrong?

Not true.

For one thing modern JVMs move objects around in memory, but do not
change the identity hash code. Recent Sun JVMs default to an algorithm
involving the object address at the time the hashCode is first
requested. However the address is not used directly as the lower bits
are not very random (the least significant three will be zero, for
instance). IIRC, the IBM JVM uses a less predictable technique.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.