I am working on a representation of a chessboard, and I plan to store it in an array with 32 bytes, where each byte will be used to store two parts. (Thus, only 4 bits are needed for each part)
By doing this in this way, there is overhead for accessing a specific board index. Do you think that this code can be optimized or you can use a completely different method of accessing indexes?
C ++
char getPosition(unsigned char* c, int index){
c+=(index>>1);
if (index & 1){
return *c & 0xF;
}else
{
return *c>>4;
}
}
void setValue(unsigned char* board, char value, int index){
board+=(index>>1);
if (index & 1){
*board = (*board & 0xF0) + value;
}else
{
*board = (*board & 0xF) + (value<<4);
}
}
int main() {
char* c = (char*)malloc(32);
for (int i = 0; i < 64 ; i++){
setValue((unsigned char*)c, i % 8,i);
}
for (int i = 0; i < 64 ; i++){
cout<<(int)getPosition((unsigned char*)c, i)<<" ";
if (((i+1) % 8 == 0) && (i > 0)){
cout<<endl;
}
}
return 0;
}
I am also interested in your opinions regarding chess representations and optimizing the above method as an independent problem.
Thank you so much
EDIT
. , 64 . , , . . -, , , .