Vector problem - square addition

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

jhaglund
Posts: 173
Joined: Sun May 11, 2008 7:43 am

Re: Vector problem - square addition

Post by jhaglund »

hmm, maybe you should init the result array

Code: Select all

Code: 

int result[] = {0,0,0,0,0,0,0,0};



OMG,

When I win the lottery, where do I make checks payable too?

:wink:
It works! Thanks a million,

Joshua
Edmund
Posts: 670
Joined: Mon Dec 03, 2007 3:01 pm
Location: Barcelona, Spain

Re: Vector problem - square addition

Post by Edmund »

jhaglund wrote:
Code:

Code: Select all

result[8] /* 0-7 => a-h */ 

for &#40;i =0; i<64; i++) 
    result&#91;i-&#40;i&56&#41;&#93;+=sq&#91;i&#93; 
 
i-(i&56)

is the same as

i & 7

I'm not familar with this technique. What is this technique called?
there isn't a name for it ...

what you want is the file from a given square ... what is used in the formula is just a bit awkward.

instead of:
result[i-(i&56)]+=sq;

you can write:
result[i&7]+=sq;

these two do the same thing.

a square is a value from 0 to 63
that is 6 bits in binary

the upper three bits denote the rank
the lower three bits denote the file

so if you want to get the file you can either

a) (like in the original code)
mask the rank and subtract it from the original leaving just the file

or
b) (like in my version)
immediately just mask the file

note that
56 is in binary notation 111000.b
and 7 is 000111.b

regards,
Edmund
jhaglund
Posts: 173
Joined: Sun May 11, 2008 7:43 am

Re: Vector problem - square addition

Post by jhaglund »

Great summary Edmund...

Thanks for your advice,

Joshua
Posted: Sun Jun 20, 2010 4:28 pm Post subject: Re: Vector problem - square addition

--------------------------------------------------------------------------------

jhaglund wrote:
Quote:
Code: Code:

result[8] /* 0-7 => a-h */

for (i =0; i<64; i++)
result[i-(i&56)]+=sq



Quote:

i-(i&56)

is the same as

i & 7


I'm not familar with this technique. What is this technique called?


there isn't a name for it ...

what you want is the file from a given square ... what is used in the formula is just a bit awkward.

instead of:
result[i-(i&56)]+=sq;

you can write:
result[i&7]+=sq;

these two do the same thing.

a square is a value from 0 to 63
that is 6 bits in binary

the upper three bits denote the rank
the lower three bits denote the file

so if you want to get the file you can either

a) (like in the original code)
mask the rank and subtract it from the original leaving just the file

or
b) (like in my version)
immediately just mask the file

note that
56 is in binary notation 111000.b
and 7 is 000111.b

regards,
Edmund