>>> operator with c langage

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

diep
Posts: 1822
Joined: Thu Mar 09, 2006 11:54 pm
Location: The Netherlands

Re: >>> operator with c langage

Post by diep »

Gerd Isenberg wrote:
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;
Heh Gerd, is Ansi-C nowadays garantueeing that in 64 bits integers right shifting of more than 32 bits is garantueed 100% ok, instead of undefined?

It's not interesting what compilers practical do, but what the language defines as being defined.

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

Re: >>> operator with c langage

Post by Gerd Isenberg »

diep wrote: Heh Gerd, is Ansi-C nowadays garantueeing that in 64 bits integers right shifting of more than 32 bits is garantueed 100% ok, instead of undefined?

It's not interesting what compilers practical do, but what the language defines as being defined.

Vincent
No idea exactly, but I guess shifts between 32 and 63 are guaranteed to be correct, even with ancient VC6 compiler. Where things are undefined are shifts > 63. X86-64 compiler usually emit shr instruction with implicit modulo 64, thus shift right 64 is a nop. In 32 bit mode a 64-bit shift right might result in null with certain compilers.

Gerd