Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: printing different values for 2 identical numbers

Author: Jaime Benito de Valle Ruiz

Date: 06:37:55 09/10/03

Go up one level in this thread


On September 10, 2003 at 08:45:22, Uri Blass wrote:

>I found that my program printed the numbers 0 and 1 when it did the following
>code without printing mistake or mistake1.
>
>zobpawnkey[hply]=zobpawn;
>if (nodes==663719)
>{
>	if (zobpawnkey[hply]!=zobpawn)
>		printf("mistake");
>	if ((zobpawnkey[hply]&1)!=(zobpawn&1))
>		printf("mistake1");
>	printf(" %d %d              ",(zobpawn&1),(zobpawnkey[hply]&1));
>}
>
>both zobpawn and zobpawnkey[hply] are __int 64 varaibles.
>
>How is it possible?
>I guess that I cannot trust & of 64 bit varaible with number unless I do some
>casting like (int)(zobpawn&1) but I thought that at least I can
>expect program to give the same value when it calculate the same thing even if
>the calculation is wrong.
>
>Uri

I think I've got a theory for you:

Some compilers, when you feed the PRINTF with 64 bit variables, they will push
in the stack 8+8=16 bytes, and they the proper routine will be called. The
function is expecting two %d, that are assumed to be 32 bit each, so the
function will take the first 4 bits (32 bits) to display one %d, and the
following 4 bits to display the second %d; and both will belong to the first 64
bit variable!!! So the info that you're reading is Wrong.
If you try this, you'll see what I mean:

  #define BITBOARD __int64
  BITBOARD a=((BITBOARD)2<<32)|1;
  BITBOARD b=3;
  printf(" %d %d              ",a&255,b&255);

You'll be expecting to see 1 (from a) and 3 (from b), but you'll see 1 2.
Acutally, you'll get the same if you just try

  printf(" %d %d              ",a,b);

because the two numbers display will be taken from the upper and lower 32 bits
of "a".
Just make sure you use:

printf(" %d %d              ",(BITBOARD)a,(BITBOARD) b);

Of course, you'll be ignoring the upper 32 bits of every varible.
Regards,

  Jaime



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.