>>> operator with c langage

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

NaltaP312
Posts: 56
Joined: Wed Oct 29, 2008 1:06 pm
Full name: Marc Paule

>>> operator with c langage

Post by NaltaP312 »

Hi,

i'm trying to code the bishopAttack and rookAttack.
to compute index with rotated bitboard i use this with java

Code: Select all

int idx_down = (int) (pieces_45 >>> DIA_DOWN_SHIFT[sq]) & 63;
int idx_up = (int) (pieces_315 >>> DIA_UP_SHIFT[sq]) & 63;
but in c, it is different.
i think i must divide by 2 my integer the number return by DIA_DOWN_SHIFT[sq].

Thanks for any help.
Best regards
Yves
Gerd Isenberg
Posts: 2250
Joined: Wed Mar 08, 2006 8:47 pm
Location: Hattingen, Germany

Re: >>> operator with c langage

Post by Gerd Isenberg »

NaltaP312 wrote:Hi,

i'm trying to code the bishopAttack and rookAttack.
to compute index with rotated bitboard i use this with java

Code: Select all

int idx_down = (int) (pieces_45 >>> DIA_DOWN_SHIFT[sq]) & 63;
int idx_up = (int) (pieces_315 >>> DIA_UP_SHIFT[sq]) & 63;
but in c, it is different.
i think i must divide by 2 my integer the number return by DIA_DOWN_SHIFT[sq].

Thanks for any help.
Best regards
Yves
While all scalar data types (char, short, int, long) in Java are signed, C distinguish signed and unsigned data types. In order to shift right with zeros from left, like Java >>>, you need to declare (or cast) the data to shift unsigned and use the same operator >> as for signed shifts, where the most significant bit (sign bit) is shifted from left.
Typically one uses a typedefinition:

Code: Select all

typedef unsigned long long  BitBoard;
or

Code: Select all

typedef unsigned long long  U64;
User avatar
Desperado
Posts: 879
Joined: Mon Dec 15, 2008 11:45 am

Re: >>> operator with c langage

Post by Desperado »

i dont know java, so i justed googled the _>>>_ operator.
So is there a reason why you dont use _>>_ operator in java.
this would also be the same in c.

_div by 2_ you can reach with c using this: _value >> 1_
User avatar
Desperado
Posts: 879
Joined: Mon Dec 15, 2008 11:45 am

Re: >>> operator with c langage

Post by Desperado »

uhps, gerd was too fast for me :lol:

he already answered what i want to know,before i was able
to post the question...
NaltaP312
Posts: 56
Joined: Wed Oct 29, 2008 1:06 pm
Full name: Marc Paule

Re: >>> operator with c langage

Post by NaltaP312 »

Hello Gerd,

yes i have defined like this
typedef unsigned long long u64;

so if i have well defined i think my bug is in another place :(
i continue to debug.

Best regards
Yves
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: >>> operator with c langage

Post by bob »

NaltaP312 wrote:Hi,

i'm trying to code the bishopAttack and rookAttack.
to compute index with rotated bitboard i use this with java

Code: Select all

int idx_down = (int) (pieces_45 >>> DIA_DOWN_SHIFT[sq]) & 63;
int idx_up = (int) (pieces_315 >>> DIA_UP_SHIFT[sq]) & 63;
but in c, it is different.
i think i must divide by 2 my integer the number return by DIA_DOWN_SHIFT[sq].

Thanks for any help.
Best regards
Yves
>>> is not a shift operator. I suspect you want ">>".

2 >> 1 turns into 1, for example.

Make certain you know what you are doing when you use signed vs unsigned.

Unsigned fills from the left with zeroes, while signed fills from the left using the sign bit itself.
NaltaP312
Posts: 56
Joined: Wed Oct 29, 2008 1:06 pm
Full name: Marc Paule

Re: >>> operator with c langage

Post by NaltaP312 »

Hello Robert,

Thanks for your reply.
what i want is to use the following:
to have a rotation right filled with zeros.

Shifts bits to the right (divide by 2 for each shift). Zeros emerging on the right are lost, while zeros are inserted to the left.

At this time i used unsigned for u64.
i suspect to have a bug in my rookAttack and bishopAttack.

Code: Select all

u64 bishopAttacks(int sq) {
	int idx_down = (int) (pieces_45 >> DIA_DOWN_SHIFT[sq]) & 63;
	int idx_up = (int) (pieces_315 >> DIA_UP_SHIFT[sq]) & 63;
	return	DIA_UP_SLIDES&#91;&#40;sq<<6&#41; | idx_up&#93; | DIA_DOWN_SLIDES&#91;&#40;sq<<6&#41; | idx_down&#93;;
&#125;

/* Returns rook-style attacks bitboard from a given square.*/
u64 rookAttacks&#40;int sq&#41; &#123;
	int idx_0 = &#40;int&#41; &#40;pieces >> (&#40;sq & 0x38&#41; | 1&#41;) & 63;
	int idx_90 = &#40;int&#41; &#40;pieces_90 >> FILE_SHIFT&#91;sq&#93;) & 63;
	return FILE_SLIDES&#91;&#40;sq << 6&#41; | idx_90&#93; | RANK_SLIDES&#91;&#40;sq << 6&#41; | idx_0&#93;;
&#125;
with this position
R2KR2R/PP1Q2NP/2PPnN2/3P1B2/3p4/2N4q/ppp2ppp/r3rk2 w - - 0 1

my result is the following for level one depth.
1 : The white king on d8 move to c8

2 : The white pawn on d5 capture the black knight at e6

3 : The white queen on d7 capture the black knight at e6

4 : The white knight on g7 capture the black knight at e6

5 : The white rook on a8 capture the black knight at e6

6 : The white rook on e8 capture the black knight at e6

so a bug for rook and a bug for king move.
but i will continue to debug and see if my precomputed table are not wrong.

Thanks
Best Regards
Yves
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: >>> operator with c langage

Post by Sven »

NaltaP312 wrote:

Code: Select all

/* Returns rook-style attacks bitboard from a given square.*/
u64 rookAttacks&#40;int sq&#41; &#123;
	int idx_0 = &#40;int&#41; &#40;pieces >> (&#40;sq & 0x38&#41; | 1&#41;) & 63;
	int idx_90 = &#40;int&#41; &#40;pieces_90 >> FILE_SHIFT&#91;sq&#93;) & 63;
	return FILE_SLIDES&#91;&#40;sq << 6&#41; | idx_90&#93; | RANK_SLIDES&#91;&#40;sq << 6&#41; | idx_0&#93;;
&#125;
In the expression "pieces >> ((sq & 0x38) | 1)", you are shifting by either 1, 9, 49, or 57, depending on which bits are set in 'sq'. Is this your intent?

Sven
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: >>> operator with c langage

Post by Sven »

Sven Schüle wrote:
NaltaP312 wrote:

Code: Select all

/* Returns rook-style attacks bitboard from a given square.*/
u64 rookAttacks&#40;int sq&#41; &#123;
	int idx_0 = &#40;int&#41; &#40;pieces >> (&#40;sq & 0x38&#41; | 1&#41;) & 63;
	int idx_90 = &#40;int&#41; &#40;pieces_90 >> FILE_SHIFT&#91;sq&#93;) & 63;
	return FILE_SLIDES&#91;&#40;sq << 6&#41; | idx_90&#93; | RANK_SLIDES&#91;&#40;sq << 6&#41; | idx_0&#93;;
&#125;
In the expression "pieces >> ((sq & 0x38) | 1)", you are shifting by either 1, 9, 49, or 57, depending on which bits are set in 'sq'. Is this your intent?

Sven
Sorry, I meant:
"you are shifting by either 1, 9, 17, 25, 33, 41, 49, or 57 ..."

(no EDIT allowed anymore :-( )

Sven
Gerd Isenberg
Posts: 2250
Joined: Wed Mar 08, 2006 8:47 pm
Location: Hattingen, Germany

Re: >>> operator with c langage

Post by Gerd Isenberg »

Sven Schüle wrote:
Sven Schüle wrote:
NaltaP312 wrote:

Code: Select all

/* Returns rook-style attacks bitboard from a given square.*/
u64 rookAttacks&#40;int sq&#41; &#123;
	int idx_0 = &#40;int&#41; &#40;pieces >> (&#40;sq & 0x38&#41; | 1&#41;) & 63;
	int idx_90 = &#40;int&#41; &#40;pieces_90 >> FILE_SHIFT&#91;sq&#93;) & 63;
	return FILE_SLIDES&#91;&#40;sq << 6&#41; | idx_90&#93; | RANK_SLIDES&#91;&#40;sq << 6&#41; | idx_0&#93;;
&#125;
In the expression "pieces >> ((sq & 0x38) | 1)", you are shifting by either 1, 9, 49, or 57, depending on which bits are set in 'sq'. Is this your intent?

Sven
Sorry, I meant:
"you are shifting by either 1, 9, 17, 25, 33, 41, 49, or 57 ..."

(no EDIT allowed anymore :-( )

Sven
Seems correct to me, to extract the six inner bits of each rank. In First Rank Attacks I shift by 0,8,16,..56 to extract 2*innersixbits in place by and with 2*63, and use a one-dimensional array to mul the already doubled index by 4 rather than 8:

Code: Select all

BYTE arrFirstRankAttacks64x8&#91;64*8&#93;; // 512 Bytes = 1/2KByte
 
U64 rankAttacks&#40;U64 occ, enumSuare sq&#41; &#123;
   unsigned int file = sq &  7;
   unsigned int rkx8 = sq & 56; // rank * 8
   occ = &#40;occ >> rkx8&#41; & 2*63;
   U64 attacks = arrFirstRankAttacks64x8&#91;4*occ + file&#93;;
   return attacks << rkx8;
&#125;