2

Conventionally, in C++, upon creating an array, I declare it as an int. However, since I am only dealing with binary numbers (1 and 0 only), I am thinking that is that possible for me to covert the 4 bytes 'int' to 1 bit.

For example

int array1[] = {1,0,0,0,0,1,0}; // -----had total of 32 bytes

Since it's only binary, the memory efficiency is just 1/32 as each int 1's will store as 000000000000000000000000000001 (4 bytes).

So my question is how can I convert all this into bit so that the 32 bytes can be 'compressed' into 1 byte (instead of 8 int of 32 bytes, I want a 8 bits data) ?

5
  • 4
    en.cppreference.com/w/cpp/utility/bitset/bitset - this should do the trick Commented Jan 9, 2016 at 14:08
  • 3
    You can use std::vector<bool> Commented Jan 9, 2016 at 14:08
  • 1
    Is the length of your array compile-time constant or only known at run time? Commented Jan 9, 2016 at 14:11
  • "had total of 32 bytes" Can you elaborate on your measuring technique? That just sounds odd. Commented Jan 9, 2016 at 15:07
  • Why not using an array of boolean in c++? Commented Jan 9, 2016 at 20:35

2 Answers 2

7

Use std::bitset. I think this is what you want.

I don't know if you are a competitive programmer but sometimes in competitive programming it is required to have 10^9 flags. Then bitset or in sieve's prime determination this is extremely helpful.

#include<bitset>
...
...
bitset<10000000> bs;

..
bs[1]=1;
bs[i]=0;
..etc
Sign up to request clarification or add additional context in comments.

5 Comments

Hi,how do i use bitset for an array?(allocating into a array)
last question, what about allocating it into multi dimensional array?
@CHONG.: I have used once using 'BOOST' library..dynamic_bitset
@CHONG Before accepting prematurely take a look at my answer. I think std::vector<bool> is more appropriate for what you want (including having multiple dimensions).
@πάντα ῥεῖ ...i do know that bool works, just that bitset is more appropriate to make it 1 bit(for my question) , bool is not one byte as i tested.
4

Conventionally, in C++, upon creating an array, I declare it as an int.

There's no such common convention.

However, since I am only dealing with binary numbers (1 and 0 only), I am thinking that is that possible for me to covert the 4 bytes 'int' to 1 bit.

Naturally one might think that this should result in declaring something like

bool array1[]{true,false,false,false,false,true,false};

Though the above implementation just reduces the space used for a single bit to unsigned char, which is the smallest memory unit that can be addressed in c++.


Fortunately c++ provides a specialization of std::vector<bool> that actually space optimizes your bit array as you want to.

8 Comments

oh dear god std::vector<bool>, just no, keep it away from us
@Puppy For the case asked for it's exactly what the OP wants. I didn't say that std::vector<bool> is a real good implementation to tackle the general problem.
@πάντα ῥεῖ std::vector<bool> size is one byte, i need it to be one bit, as i am doing millions time of iterations, i think that fetching 1 bit compare to one bytes millions time should improve some of its performance, just trying out.
@CHONG "std::vector<bool> size is one byte" No, it's size isn't one byte. You misunderstood, std::vector<bool> is space optimized, and allocates as many bytes needed to fit your bits in.
@CHONG "as far as i tested, for a e.g length of 32" How did you test it?sizeof(std::vector<bool>) just fools you here. It's not the size of the actual data kept in the vector.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.