# Converting 64 bits to two 32 bit integers

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

## 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 on other sites
Why not use a native 64-bit integer type? They're pretty fast, really.

##### Share on other sites
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 on other sites
Excellent. Thanks, you guys. That's going to be a big help.

##### 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 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 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 on other sites
Use std::bitset. It will be faster, cleaner, and more convenient than anything you could come up with yourself.

##### 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.

1. 1
2. 2
Rutin
19
3. 3
khawk
19
4. 4
5. 5
A4L
11

• 9
• 12
• 16
• 26
• 10
• ### Forum Statistics

• Total Topics
633771
• Total Posts
3013761
×