merging bits

This is a discussion on merging bits within the C++ Programming forums, part of the General Programming Boards category; I have two arrays of UINT32's that look like this. Code: vector<UINT32> a; vector<UINT32> b; I'm using them to store ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    70

    merging bits

    I have two arrays of UINT32's that look like this.
    Code:
    vector<UINT32> a;
    vector<UINT32> b;
    I'm using them to store bits. Here's my problem.

    Let's imagine both a and b have 48 bits stored in them, so they both have size()==2. But only half of the second UINT32 is actually being used as bits. I need to insert b onto a, but I want to insert it on the bit level. So after the insertion is complete the result should be size()==3 and 96 bits total. The bits must also stay in the same order as they are in currently, so no swapping. The problem is that I have no idea how to do this, but it seems like there must be a way. If anybody needs clarification on anything just ask.

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    why would you want to thwart the idea of container classes by bypassing their access methods?

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    70
    I believe I have actually found my solution in Guru Of The Week 72.
    http://www.gotw.ca/gotw/072.htm

    @m37h0d
    The way I'm looking at it I need to. I'm only using vector for its memory management help. If I were to use vector<UINT32>::Insert to add b to a there would be 16 bits between left unused.

    edit: It looks like I'm going to end up using vector<bool> or bitset though.
    edit2: make that vector<bool>
    edit3: make that dynamic_bitset
    Last edited by Drac; 12-21-2008 at 01:59 PM.

  4. #4
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    i'm sure laserlight, et al. will disagree, but to me it seems easier to use char * and do the memory management yourself imho.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    It is not. Especially not when vector<bool> does it all for you. You think like a C programmer. What are you doing here?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Isn't vector<bool> deprecated?

    dynamic_bitset (boost has one but is it going to make into C++0x?) seems like a good choice.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    Quote Originally Posted by Elysia View Post
    It is not. Especially not when vector<bool> does it all for you. You think like a C programmer. What are you doing here?


    shrug.
    Last edited by m37h0d; 12-21-2008 at 06:11 PM.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This seems to miss a lot of functionality that you'd expect from a bitset (most notably copy constructor and assignment operator). See here for what boost::dynamic_bitset has. Then think if you want to implement all that all over.

    Your implementation also suffers from a number of problems, such as mixing realloc and delete[] (and using realloc in an unrecommended way - and reduplicating code that does reallocing). And what's with the C-style casts to T*. Any good coming if T happens to be something like std::string? And it isn't really a bitbuffer, as it appears to use one byte per bit.
    Last edited by anon; 12-21-2008 at 05:38 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    yeah, true. it was more an example of how i thought the problem could be tackled much more simply than that GOTW page. but i shouldn't post things that aren't totally correct, especially if i'm not really interested in doing it correctly to a T (pun intended).

    and despite my unfortunate naming, it wasn't necessarily a bitbuffer so much as it was directed at the OP's problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SDLKey to ASCII without unicode support?
    By zacs7 in forum Game Programming
    Replies: 6
    Last Post: 10-07-2007, 04:03 AM
  2. Replies: 7
    Last Post: 08-19-2007, 09:10 AM
  3. Help counting number of bits set in an integer
    By JayDiddums10 in forum C Programming
    Replies: 5
    Last Post: 12-07-2006, 03:21 PM
  4. Writing binary data to a file (bits).
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 04:53 PM
  5. New idea on conveting byte to bits/bits to byte
    By megablue in forum C Programming
    Replies: 10
    Last Post: 10-26-2003, 01:16 AM

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