Thread: int into bits Bitwise

  1. #16
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #17
    Registered User
    Join Date
    May 2010
    Posts
    21
    i just looked at the example, but i dont understand how that packing them as bits to send?

  3. #18
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by MK27 View Post
    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;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #19
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by bdillion View Post
    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?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #20
    Registered User
    Join Date
    May 2010
    Posts
    21
    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. #21
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by bdillion View Post
    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.
    Last edited by MK27; 05-18-2010 at 02:59 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #22
    Registered User
    Join Date
    May 2010
    Posts
    21
    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. #23
    Registered User
    Join Date
    May 2010
    Posts
    21
    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
    Last edited by bdillion; 05-18-2010 at 03:02 PM.

  9. #24
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by bdillion View Post
    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!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #25
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by bdillion View Post
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #26
    Registered User
    Join Date
    May 2010
    Posts
    21
    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. #27
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by bdillion View Post
    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!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  13. #28
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by bdillion View Post
    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.
    Last edited by MK27; 05-18-2010 at 03:46 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #29
    Registered User
    Join Date
    May 2010
    Posts
    21
    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. #30
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    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
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  3. Replies: 26
    Last Post: 11-30-2007, 03:51 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM