Jump to content
  • Advertisement
Sign in to follow this  
silverphyre673

Converting 64 bits to two 32 bit integers

This topic is 4702 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've had an idea for how to implement a "learning" AI for my Othello game. For it, it needs to examine the board (8x8) and determine, for each square, whether it can or cannot place a piece there. The AI sorts out its best options directly based on this. Now, I COULD have it match 64 boolean variables to some "how good is this move" table, but that would be extremely inefficient, especially in terms of memory usage. I was wondering if anyone could tell me a way that these 64 bits could be somehow turned into two 32 bit integers, which would make it easier to store and faster to compare. By the way, I'm using C++. Thanks a bunch!

Share this post


Link to post
Share on other sites
Advertisement
unsigned int value = (bit0 << 0) + (bit1 << 1) + (bit2 << 2) .. (bitN << N);
bit0 = (value >> 0) & 1;
bit1 = (value >> 1) & 1;
bit2 = (value >> 2) & 1;
..
bitN = (value >> N) & 1;

Obviously, one simply splits the 64 into two chunks and assigns to seperate variables :-).

Share this post


Link to post
Share on other sites
Alternately, you can probably do an ugly static cast with a pointer to the 64 bit integer. But I wouldn't recommend that, just noting it down as a further option.

Share this post


Link to post
Share on other sites
A bit easier than messing with shifting IMO, you can use unions


typedef struct
{
union
{
__int64 asInt64;
struct
{
int asInt32a;
int asInt32b;
};
};
} MyType;

MyType foo;

// since it's a union you can manipulate the 64 bit value, or either of the 32 bit ones.
foo.asInt64 = 8947238473824;
foo.asInt32a = 34;
foo.asInt32b = 3453;


Depending on compiler you may have to change __int64 to long long or whatever. This was whipped up in msvc7.1. And you may have to name the union depending on compiler.

Share this post


Link to post
Share on other sites
I was under the impression he wanted to figure out how to assemble his collection of bits into unsigned integers to manipulate, not cast from integer type to integer type.

But now that I've woken up more, I should also suggest std::bitset (first google entry for the term, I know SGI's docs arn't the best but meh...)

Share this post


Link to post
Share on other sites
I got it working. It isn't really important that it be super fast - it's a board game, and the AI isn't doing like a tree search or anything. This is almost just a brute force solution. I wound up just using the shifts to create two integers, and that worked well. Thanks to all who responded.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!