# Thread: int into bits Bitwise

1. Nb, just corrected some mistakes in my last post, went a bit hard on the coffee this afternoon

The set & unset functions are not very good (unset toggles) but this gives the general idea behind bitflaging.

2. i just looked at the example, but i dont understand how that packing them as bits to send?

3. Originally Posted by MK27
Nb, just corrected some mistakes in my last post, went a bit hard on the coffee this afternoon

The set & unset functions are not very good (unset toggles) but this gives the general idea behind bitflaging.
Right, so what you want is basically:

Code:
```void unset (int flag)
{
field &= ~flag;
}```

4. Originally Posted by bdillion
i just looked at the example, but i dont understand how that packing them as bits to send?
Well, did you read my post (with respect to the "state ambiguity")? How do you plan to imbed 3 states in a boolean (eg: binary) value?

5. I dont know i'm just using the code provided and trying to do what im told, i have 0 idea what im doing and i think i might fail this tbh i just dont understand it how you pack it and send it

6. Originally Posted by bdillion
i just looked at the example, but i dont understand how that packing them as bits to send?
Okay, this is the part where you have become confused: this has nothing to do with sending over the network, except in so far as it also saves space there. You just transmit the whole field as is. It's an int. An int is (usually) 32 bits. The bits are in the int already. You don't have to do anything further.

The "memory optimization" involved is because you can have 32 boolean flags in a single int. Since you need two flags per square, that's 16 squares in an int. There is some processor optimization too since there is less loading of values, and AND/OR are single operations in asm.

So if you are sending over a network, all that becomes compounded: only four bytes have to be transmitted, only 4 have to be received each turn, because the entire state of the board is contained in that one int. This is why it was recommended to you as a method. But it is still sent and recieved as an int. It is meaningless to talk about sending individual bits since that is not possible. The smallest unit you can send via the network layer is a byte. And you cannot "compress" bitfield information* because that is as compressed as it logically could be.

* well, with a very large one you could look for patterns, etc, as in normal compression. But I promise: no sane person would recommend doing that with one single int. You could trim the high zeros out but then you would have to add more information to indicate the size of the data, which will make it bigger than it started -- seriously, you are confused about this point.

7. but then how do i know how to pack them all im getting is outputs to the screen saying this is x this o, im really lost.

8. also i incorporated the example and it seems to think everything is an x

public:
int field;

bitwiseNoughtsAndCrosses () { };
bool check (int flag) {
if (field & flag) return true;
return false;
}
void set (int flag) {
field |= flag;
}
void unset (int flag)
{
field &= ~flag;
}
~bitwiseNoughtsAndCrosses() { };

void Packing()
{

bitwiseNoughtsAndCrosses example;

example.set(SQUARE1_O);
example.set(SQUARE2_X);

if (board[1] = (SQUARE1_X)) cout << "S1 is X\n";
else if (board[1]=(SQUARE1_O)) cout << "S1 is O\n";
else cout << "S1 is empty\n";

so im saying if board position 1 is a x write its an x

9. Originally Posted by bdillion
also i incorporated the example and it seems to think everything is an x
<snip>
so im saying if board position 1 is a x write its an x
Sorry, but that code doesn't even really make sense! Okay, so I'm guessing you don't have a very firm grasp of the language, right? Well, as tough as it sounds, your best bet right now is head to the local bookstore/library and do some serious self-study. Once you have a grasp of the basics, you'll be ready to dive into the project. But until then, you should probably put this project on the back-burner.

Good luck!

10. Originally Posted by bdillion
also i incorporated the example and it seems to think everything is an x
Ah, well, I did not really mean for you to use the class, it was just a demonstration of the concept.

However, you could play with the demo program/tailor it yourself until you get the idea. It fairly simple once you understand, but I can remember being sort of intimidated by bit ops when first introduced to them.

You just have to focus on the fact that all data is stored in binary, that the byte is the smallest functional unit in C/C++, and there are 8 bits in a byte. Here's another approach to this that doesn't involve any of the bit manipulation syntax:
Code:
```#include <iostream>

#define SETX 1
#define SETO 2

struct bitfield {
char s0:2;
char s1:2;
char s2:2;
char s3:2;
};

using namespace std;

int main(int argc, const char *argv[]) {
struct bitfield eg;

cout << "struct bitfield is only " << sizeof(struct bitfield) << " byte(s).\n";
eg.s1 = SETX;
eg.s3 = SETO;

if (eg.s1 == SETX) cout << "Square 1 is X\n";
if (eg.s2 == SETX) cout << "Square 1 is X\n";

return 0;
}```
:2 means this member only needs two bits. The compiler will pad this out to a byte level, but since we have four, the struct will still only be the size of a single char. AFAIK, you can't have an array of these, so it is somewhat awkward for this purpose.

Don't freak out. And do play with this in a small program separate from your project, it will make you life much easier.

11. unfortunately i dont have time to read a c++ book we've been given 4 days to do this, we were taught very badly c++ this year and were expected to do this after about 5 hours of coding.
so im am kinda freaking out

12. Originally Posted by bdillion
unfortunately i dont have time to read a c++ book we've been given 4 days to do this, we were taught very badly c++ this year and were expected to do this after about 5 hours of coding.
so im am kinda freaking out
Sure, I understand. Sometimes we end up with lousy professors. All I can say is: take a bad mark on this one, study like hell, and hopefully by the time your *next* project is due you will have enough understanding to tackle your assignments. In the meantime, though, it just isn't reasonable to expect us to teach you the language "from the ground up"...

Cheers!

13. Originally Posted by bdillion
unfortunately i dont have time to read a c++ book we've been given 4 days to do this, we were taught very badly c++ this year and were expected to do this after about 5 hours of coding.
so im am kinda freaking out
Yes, I know the feeling. This is why you think the fastest route is to try and write everything into the project. Trust me, it isn't. You will just make a mess. Take an hour or two, maybe use that first demo class I gave you (or whatever) and focus just on bit manipulation. Maybe add a simple menu and input so you can set and unset squares and see the state of the field afterward Don't worry about the main code. Go back to that once you are comfortable with the concepts. You can also use your experiment later if something in the main code doesn't work the way you were expecting.

This will be worth it, because IMO that bitfield will be very central to how your whole thing works. Remember, the bitfield IS the board.

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

15. I am wondering why you are not using bitpacking for this...way simpler and shorter code. For one thing you are wasting two bits on each value when one will do (since it can only be X or O) so you could easily set up something like this:
Code:
```struct XandO
{
char Field00:1
char Field01:1
char Field02:1
char Field10:1
char Field11:1
char Field12:1
char Field20:1
char Field21:1
char Field22:1
// represents 9 bits, one for each square. X = true, O = false
};

// 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 bit, say in the middle of the board, at coords 1,1:
playState.Field11 = true;

// To check that state of say 0,0 is just the opposite:
bool IsAnX = playState.Field00;

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

// thats it```

Popular pages Recent additions