Problem with unsigned int

This is a discussion on Problem with unsigned int within the C Programming forums, part of the General Programming Boards category; In the following code: Code: for (int i = 0; i < 64; ++i) { if (posicion[jugada_actual].piezas[i] != VACIO) hash ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    145

    Problem with unsigned int

    In the following code:
    Code:
    	for (int i = 0; i < 64; ++i) {
    		if (posicion[jugada_actual].piezas[i] != VACIO)
    			hash ^= hash_piezas[posicion[jugada_actual].color[i]][posicion[jugada_actual].piezas[i]][i];
    	}
    if I change 'int i=0' for 'unsigned int = 0' I dont get the same result. In fact, the ^ operation result bad. Any ideas why?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Seeing as i is never negative, I don't see what difference it makes if it's signed or unsigned.

    The ^ operator performs XOR, which is unlikely to be flawed in a compiler.

    I do notice that hash is not initialized in the snippet of code you posted - is it possible that this is causing a problem?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    as a rule you should always use unsigned when using bit operators so Im pretty sure the problem is somewhere else in the code. Maybe as matsp pointed the hash not initialized

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    Quote Originally Posted by matsp View Post
    Seeing as i is never negative, I don't see what difference it makes if it's signed or unsigned.

    The ^ operator performs XOR, which is unlikely to be flawed in a compiler.

    I do notice that hash is not initialized in the snippet of code you posted - is it possible that this is causing a problem?

    --
    Mats
    This looks like a Zobrist-key hash function from a game playing program. The hash value is pre-existent, and gets updated with the board state... Looks fine to me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  2. Replies: 3
    Last Post: 05-13-2007, 09:55 AM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 10:16 PM
  4. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21