Reputation: 155
I am programming a Reversi game in C, and i am new to the language (coming from java),and also have never programmed a game with 8x8 board before. I want to use bit Boards to represent the game board for both white and black players (one 64 bit word for each), and i wonder if i should use an Unsigned long long for that purpose.
From what I know, an Unsigned Type is a one that doesn't use the leftMost bit as a sign indicator (0 for positive and 1 for negative), I also know that java for example,support only signed Types. but in my case, I would need to use the left most bit as a valid square for the board. Does it matter if i use a signed type or unsigned type for that??
for example, if i place a white piece on the last square of the white bitBoard (the left most bit) and the number will become negative, is it ok?
Upvotes: 1
Views: 238
Reputation: 22946
Yes, if you're doing things with bits, you'd better use unsigned.
Note that unsigned long long
is guaranteed to be at least 64 bits, by the C standard.
Have a look at bit-fields, very handy in your case; saves you bit-fiddling with &
, |
, ^
, ...
But here's an idea:
#include <stdint.h> // Thanks @DoxyLover
typedef struct
{
uint8_t a : 1;
uint8_t b : 1;
uint8_t c : 1;
uint8_t d : 1;
uint8_t e : 1;
uint8_t f : 1;
uint8_t g : 1;
uint8_t h : 1;
} BoardRow;
typedef struct
{
BoardRow r1;
BoardRow r2;
BoardRow r3;
BoardRow r4;
BoardRow r5;
BoardRow r6;
BoardRow r7;
BoardRow r8;
} Board;
void someFunction(void)
{
Board board;
board.r5.d = 1;
...
// You can save these board in uint64_t if you like.
uint64_t savedBoard = (uint64_t)board;
...
}
Upvotes: 2