int into bits Bitwise

This is a discussion on int into bits Bitwise within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by jeffcobb For one thing you are wasting two bits on each value when one will do (since ...

  1. #31
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jeffcobb View Post
    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...

    Quote Originally Posted by bdillion View Post
    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"?
    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. #32
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    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...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  3. #33
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jeffcobb View Post
    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...
    Last edited by MK27; 05-18-2010 at 07:01 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

  4. #34
    Registered User
    Join Date
    May 2010
    Posts
    21
    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. #35
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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).
    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

  6. #36
    Registered User
    Join Date
    May 2010
    Posts
    21
    with this method can i pack the whole thing ? i mean how does the fields know whats in them without logic like that

  7. #37
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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...
    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;
    }

  8. #38
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by bdillion View Post
    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..
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  9. #39
    Registered User
    Join Date
    May 2010
    Posts
    21
    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. #40
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    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...
    Last edited by jeffcobb; 05-19-2010 at 03:09 PM.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  11. #41
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by bdillion View Post
    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.
    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;
    }

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

  14. #44
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by bdillion View Post
    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...
    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;
    }

  15. #45
    Registered User
    Join Date
    May 2010
    Posts
    21
    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

Page 3 of 5 FirstFirst 12345 LastLast
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, 06:26 PM
  2. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 11: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, 09:54 PM
  5. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 06:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21