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; emailed my lecturer and he said 2 bytes is all u need 1 for each position 1 for each symbol, ...

  1. #61
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,147
    emailed my lecturer and he said 2 bytes is all u need 1 for each position 1 for each symbol, however 9 squares 9 potential symbols 9x9 = 18 thats 2 bytes and 2 bits?
    Let your lecturer know that he is an idiot. If I've done the little mathematics needed and accounted for mirroring correctly, you only 15 bits.* Are you expected to save that last bit as well?

    I have contributed all that I can to this goose chase; with about 10 lines of code he could take what I gave him and be done.
    O_o

    Yep. I read this stupid thread thinking I might find something interesting. I found a solved problem and a pedantic fool causing confusion. Lovely.

    Soma

    *Edit: I just realized that a much easier option exists. ;_;

  2. #62
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by phantomotap View Post
    If I've done the little mathematics needed and accounted for mirroring correctly, you only 15 bits.
    That's terrific but so far it has not been done in any number of bits. I think the OP should focus on the basic procedures here and then if s/he has time left can consider some potential optimizations.
    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

  3. #63
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Quote Originally Posted by jeffcobb View Post
    I have an IPC server and an autoptr class to get going by tomorrow...
    Not sure what kind of smart pointer you need, but Boost has a pretty decent set of them, IMO. They don't have a non-shared, non-slicing version, though...but if you do need something like that, you're welcome to use mine (100% unrestricted license, of course).
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  4. #64
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,147
    That's terrific but so far it has not been done in any number of bits. I think the OP should focus on the basic procedures here and then if s/he has time left can consider some potential optimizations.
    O_o

    I don't know how my comments may have confused you; I'll try to be clear this time.

    This, the above quote, is exactly my point. The suggestion offered so far present a clear and obvious solution to this problem. Those suggestions are what the OP should be working with.

    At this level of educational development it is misleading at best to try and force an obscure representation when it clearly has nothing to do with the stated goal of understanding bit manipulation and integer packing.

    [Edit]non-slicing version[/Edit]

    O_o

    What?

    Soma
    Last edited by phantomotap; 05-19-2010 at 07:46 PM.

  5. #65
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by Sebastiani View Post
    Not sure what kind of smart pointer you need, but Boost has a pretty decent set of them, IMO. They don't have a non-shared, non-slicing version, though...but if you do need something like that, you're welcome to use mine (100% unrestricted license, of course).
    Actually I have that. It was my thought to include into it the possibility of overloading the [] operator and leveraging internal information to catch buffer under and over runs. Seems kinda obvious but as I mentioned in another thread I am trying to squeeze all the extra debugging tricks/info out of standard and semi-standard tools that I can. Probably a fools errand but I thought to try...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  6. #66
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by phantomotap View Post
    O_o

    I don't know how my comments may have confused you; I'll try to be clear this time.

    This, the above quote, is exactly my point. The suggestion offered so far present a clear and obvious solution to this problem. Those suggestions are what the OP should be working with.

    At this level of educational development it is misleading at best to try and force an obscure representation when it clearly has nothing to do with the stated goal of understanding bit manipulation and integer packing.

    [Edit]non-slicing version[/Edit]

    O_o

    What?

    Soma
    Interesting. The smallest number of bits I was able to get it done in was 18 (9 cells with three possible states each). I will be interested to see this...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  7. #67
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,147
    Nope. I'll be damned if I'm going to be responsible for leading the OP to such a confusing path.

    Soma

  8. #68
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by phantomotap View Post
    Nope. I'll be damned if I'm going to be responsible for leading the OP to such a confusing path.

    Soma
    The OP does seem to be scoring about a 12 on the confusion meter. Part of his problem is stress, part of it is lack of experience and part of it is several of us all submitting working yet different approaches to his problem. That is another reason I backed out of the conversation.

    It did bring something to mind though...has this forum ever done friendly contests to see who can submit the best solution to various problems like this? I think it would be a hoot for us more experienced folk and educational for the more junior members...and it would also do away with this sort of confusion...just a thought.

    Anyhow back to my IPC system. On a roll and want to have something working before my head hits the pillow tonight...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  9. #69
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Quote Originally Posted by phantomotap View Post
    [Edit]non-slicing version[/Edit]

    O_o

    What?
    It just allows you to clone an object (that you only have a pointer to the base class to) without it getting "sliced". All of the ones that I had seen required a 'clone' method - kind of a kludge, IMO. So I decided to work on the problem myself, and finally came up with a way to do it that only requires the class to be copy-constructible. Plus, as a bonus, each object internally uses just a single pointer as data, so it's as light-weight as, say, an std::auto_ptr (well, not quite - each class that uses the template creates an addition pointer of overhead, but hey).
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  10. #70
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Quote Originally Posted by phantomotap View Post
    Let your lecturer know that he is an idiot. If I've done the little mathematics needed and accounted for mirroring correctly, you only 15 bits.* Are you expected to save that last bit as well?



    O_o

    Yep. I read this stupid thread thinking I might find something interesting. I found a solved problem and a pedantic fool causing confusion. Lovely.

    Soma

    *Edit: I just realized that a much easier option exists. ;_;
    Just skimming over this thread, and if the goal is to represent all possible tic-tac-toe boards in the least space, then 15 bits is the typical answer I would expect. There are exactly 3^9 = 19683 possible board combinartions if you assume that the number of X's and O's can differ by more than one.

    You can get it down to 14 bits if you don't represent all the possible boards that contain a difference between the X's and O's of more than one. Not worth doing unless one is writing a compression algorithm of course.

    Is there anything left to answer here?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #71
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,147
    I decided to work on the problem myself, and finally came up with a way to do it that only requires the class to be copy-constructible.
    I see. I'm an idiot. When I read your post my thoughts were "What business does a smart pointer have being polymorphic?".

    I'm just guessing, you store the underlying object by indirection through a proxy typed from a templated constructor of the smart pointer class and clone the proxy instance when you need a copy of the underlying object?

    You can get it down to 14 bits if you don't represent all the possible boards that contain a difference between the X's and O's of more than one.
    This would be the easier option I referenced. My first thoughts were needlessly complicated by considering the number of possible game variations as "pictures" instead of an ordered set of ternary values.

    Not worth doing unless one is writing a compression algorithm of course.
    O_o

    If you are going that far, you could squeeze even more out of those records in the average case with some form of variable length encoding.

    Is there anything left to answer here?
    If the OP has been truthful, how does the lecturer have a job?

    Soma

  12. #72
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by iMalc View Post
    Is there anything left to answer here?
    Yeah -- who on earth would consider saving 3 bits worth any kind of thought at all? IMO that whole discussion constitutes sabotaging the thread, since you are clearly doing the OP no kind of favour. Real nice. Are we supposed to be impressed?
    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

  13. #73
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Quote Originally Posted by phantomotap View Post
    I see. I'm an idiot. When I read your post my thoughts were "What business does a smart pointer have being polymorphic?".

    I'm just guessing, you store the underlying object by indirection through a proxy typed from a templated constructor of the smart pointer class and clone the proxy instance when you need a copy of the underlying object?
    Basically, yes - each class generates a global allocator object that handles construction/destruction, and a pointer to this object is stored in the smart pointer (so two pointers are actually required, not one, as I claimed earlier). Another cool thing about it is that the base class doesn't even have to have a virtual destructor.*

    Oh, and sorry for the OT discussion, BTW...

    *I do realize that no sane programmer would define a base class having a non-virtual destructor. It's just that I've always had this fascination with writing code that overcomes limitations of the language, and one aspect of C++ that always irked me was the fact that destructors aren't virtual, by default. So when I realized that the mechanism for that could be added with little overhead, I went for it, naturally.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  14. #74
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    So...what did the OP eventually do?
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  15. #75
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Quote Originally Posted by MK27 View Post
    Yeah -- who on earth would consider saving 3 bits worth any kind of thought at all? IMO that whole discussion constitutes sabotaging the thread, since you are clearly doing the OP no kind of favour. Real nice. Are we supposed to be impressed?
    Huh?!
    That was a genuine question. What question are we currently supposed to be answering? I haven't read the whole thread and I'm not going to go back and read all five pages now. I'm genuinely trying to work out if there's reason the OP hasn't featured in any of the posts on this page. I don't know what you're on about.

    At the top of this page I'm seeing a question about whether a ttt board can be represented in only two bytes. I'm simply confirming that it can. As for why anyone would want to, I dunno.
    Last edited by iMalc; 05-21-2010 at 02:21 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Page 5 of 5 FirstFirst 12345
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, 02: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

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