# Thread: int into bits Bitwise

1. Originally Posted by jeffcobb
For one thing you are wasting two bits on each value when one will do (since it can only be X or O)
Ixnay Jeff; last time I played tick tack toe you start with a board with no X's or O's. So there are three possibilities per square, not 2. At least, this is what I have been presuming...

Originally Posted by bdillion
ok i understand the concept how how its trying to work all x and o are given a number x = 1 0= 0 anbd the position is also given a number 1= full 0= empty so x in position xero would be 11 and if no other spaces are full it would be 110000000000000000 16 bits 2 bytes, now im just confused how to actually make it so that it knows it full or empty
What do you mean by "full or empty"?

2. Erm it has been a long time since I played that. You are correct. Still my solution is sound, just change the bitpacking to
char Field00:2
char Field01:2

And use three states. I mean you have 32 bits to play with...

3. Originally Posted by jeffcobb
Erm it has been a long time since I played that. You are correct. Still my solution is sound, just change the bitpacking to
char Field00:2
char Field01:2

And use three states. I mean you have 32 bits to play with...
Yo dude that was post #25

I'm sure bdillion will figure it all out in time. I dunno about 4 days tho...mebbe someone under-estimated some things...

4. jeffcobb-

just trying out your idea, when you set if its true or false

playState.Field00 = true;

is there a way i can find out what is x and what is o before. i tried

if (board[0] == playerSymbol)
{
playState.Field00 = true;
}

but it seems to think pos 0 in the array is full when it is not

5. Umm -- if each square can be either X or O or empty (like tic tac toe), you cannot use that method. It will work if all the squares are either X or O and never "empty" (you never explained the rules of the game).

6. with this method can i pack the whole thing ? i mean how does the fields know whats in them without logic like that

7. If you really want help, you're going to have to learn to pay attention to what people are saying to (and asking of) you WRT nailing down the requirements. You seem to either gloss over, or simply ignore requests for clarification (a good example is my post, #19, and then your "response", post #20). Sorry, but "I don't know" just doesn't cut it...

8. Originally Posted by bdillion
with this method can i pack the whole thing ? i mean how does the fields know whats in them without logic like that
I don't understand your concern; I would amend my solution (based on MKs reminder that there can be a third state in X and O (I last played this game as a child, far longer ago that I would like to admit) thusly:
Code:
```typedef enum {stateEmpty, stateX, stateO} XandOStates;
struct XandO
{
char Field00:2
char Field01:2
char Field02:2
char Field10:2
char Field11:2
char Field12:2
char Field20:2
char Field21:2
char Field22:2
// represents 18 bits, two for each square. Empty = 0, X = 1, O = 2
// still plenty small enough to fit into a single int.
};

// then do a union masking an int (memset to zero the extra bits are ignored)
union TPlayState
{
int packedPlayState;
struct XandO playBits;
};

// in your code create an instance
union TPlayState playState;

// memset this to zero. Normally it would take a C call but now:
int ZeroState = 0;
playState.packedPlayState = ZeroState;

// then to set a Field, say in the middle of the board, at coords 1,1:
// not sure how you are marking your fields but lets say you have a 3x3 grid
// each grid element is a char that is one of: Empty = ' ', X = 'x' and O = 'o'

char XandOBoard[3][3];
// init all to empty
int x;
for(x = 0; x < 3; x++)
{
int y;
for(y = 0; y < 3; y++)
{
XandOBoard[x,y] = ' '; // set to empty
}
}
// note: memset would work well here too:
memset(XandOBoard, ' ', 9);

// anyhow you have some game play and want to pack the board...
// I could put the logic here but to be sane I will stick it into a function
playState.Field11 = getLogicState(XandOBoard[1][1]);

// To check that state of say 0,0 is just the opposite:
XandOState state = playState.Field00;
// set the boardmember with another function that is the opposite of
// getLogicState()
XandOBoard[0][0] = setLogicState(state);

// Finally so make an int to xfer over the network
int totalPlayState = playState.packedPlayState;

// thats it, send totalPlayState over the wire..

}

char getLogicState(char)
{
char cReturnValue = stateEmpty;
switch(char):
{
case 'x':
{
cReturnValue = stateX;
break;
}
case 'o':
{
cReturnValue = stateO;
break;
}
}
return cReturnValue;
}
char setLogicState(XandOState state)
{
char cReturnValue = ' ';
switch(state)
{
case stateX:
{
cReturnValue = 'x';
break;
}
case stateO:
{
cReturnValue = 'o';
break;
}
}
}```
I was sorta kinda hoping you could extrapolate this from the previous post but since not, here it is spelt out in 1s and 0s..

9. i am not simply glossing over it, your speaking to me as if i've been coding for years which i have no. I do not gloss over your suggestions i just dont understand you, dont misinterpret what i say. I clearly am lacking and understanding here and am asking questions based on what people post to get a better idea, your comments like that and the read a book ones are simply not useful at all as a: i dont have time and b: they do not help with my understanding. I appreciate you trying but if you have comment like that in future please hold your tongue

10. Dude cool your jets; he was referring to the requirements, specifically that each cell/square could be X, O or Empty (its that third state that I forgot having not played this in like 40 years and thus came up with the boolean true/false marker in error). He was just saying that if you had stated that up-front (nothing to do with coding) that I would not have made the mistake. Me, I am more ashamed of my dodgy memory for such a simple childs game.

In any event, the solution I pasted above will work; obviously you will need to do some of the leg-work in it but I showed how to pack/unpack the bits, how to convert a board value into a bitfield and back again. Its up to you to set all the board values and all the states and do the socket work which should be falling down simple....google for "beej sockets" if confused...

Jeff

PS: to get a firmer grasp on how this works take the above and wrap it in a simple main() console project and run it to get the feel for how it works and what you have left to do to finish the task...

11. Originally Posted by bdillion
i am not simply glossing over it, your speaking to me as if i've been coding for years which i have no. I do not gloss over your suggestions i just dont understand you, dont misinterpret what i say. I clearly am lacking and understanding here and am asking questions based on what people post to get a better idea, your comments like that and the read a book ones are simply not useful at all as a: i dont have time and b: they do not help with my understanding. I appreciate you trying but if you have comment like that in future please hold your tongue
Look, I'm not going to sugar-coat my responses just because you don't like to be criticized. You're unprepared for your assignment. Guess what? Your fault! Your responses are vague and so we have a hard time understanding what exactly you're wanting to do. Guess what? Your fault!

The bottom line is this: you asked for the advice and you got it - what you do with it is up to you.

12. I accept criticism however you are not helping your being an elitist prick, the other people in this thread are more than willing to explain it in terms i understand that i thank them for it so just stay out of this thread if you have nothing useful to say, guess what i know i was unprepared so is my whole class considering no one can get it to work due to poor teaching. Not my fault
perhaps i could have done additional reading but unfortunately given 4 days to do it i'm trying my hardest to read and work on something that is considered an advanced topic. I know professional coders who barely use this or need it

13. Uh I am a professional developer (inspite of what Mario would have everyone believe) and I use bit-packing all the time. This is not really an advanced topic (bitpacking in and of itself) but some of the design-level stuff can be. I tend to do more in the realm of embedded stuff so maybe that is why I do more of this. However to paraphrase Kennedy (the pres. not the guy here): You will find two classes of coders: one that looks at the requirements and finds a reason it won't work and another class that looks at them to find a way to make them work. Be one of the latter and you will always be sought-after.

14. Originally Posted by bdillion
I accept criticism however you are not helping your being an elitist prick, the other people in this thread are more than willing to explain it in terms i understand that i thank them for it so just stay out of this thread if you have nothing useful to say, guess what i know i was unprepared so is my whole class considering no one can get it to work due to poor teaching. Not my fault
perhaps i could have done additional reading but unfortunately given 4 days to do it i'm trying my hardest to read and work on something that is considered an advanced topic. I know professional coders who barely use this or need it
Translation:

"I can accept criticism, unless of course it hurts my feelings. I would rather be spoon-fed answers than to actually have to think hard about the problem. I know I'm unprepared, but I'd rather blame my teacher than take responsibility for my own education. Perhaps I could have started reading last year (when I started this class), but now it's too late, and so I'm hoping these people will help me achieve the impossible. I bet professional programmers don't even need to understand binary operations."

What a maroon...

15. another thrilling wrong addition by yourself there i think i proved my own point that your too up yourself to accept criticism yourself. I'm sure your go far with that attitude