Binary Combination of a given N

Hello everyone,
I need a small help. I need to store binary combination of a given a positive integer number N in a vector. Say N=2, so I need to obtain first
0 0
0 1
1 0
1 1
and I need to have T number of these vectors by putting the T in front of these vectors. Say T= 2, so when firs indice is equal to 0;
0 0 0
0 0 1
0 1 0
0 1 1
when the first indice is equal to 1( T=2)
1 0 0
1 0 1
1 1 0
1 1 1

How can I do that ? Thank you so much in advance


so, the first one is 0,1,2,3 ... which is for 0 to 2^N - 1.
the second is is also 0,1,2,3 ... just with a leading pointless zero.
the third one is 4,5,6,7
...
this is an integer problem. you can store the integers as integers or iterate them and push them into bit storage (std::bitset) to get the 'binary' values.
Last edited on
Thank you @jonnin. I could not understand what you meant exactly. If the given number is 2, then I need to generate a vector with 3 element like V={0, 0, 0} . Actually, it does not have to be a vector. In fact I am trying to generate decision variables. So say the name of the decision variable is V[x][y][j]. x will be the values from x=0 to T. y and j will be the binary combinations. For example when x=0 and N=2 then,
V[0][0][0]
V[0][0][1]
V[0][1][0]
V[0][1][1]

when x=1

V[1][0][0]
V[1][0][1]
V[1][1][0]
V[1][1][1]

so on until T
I know bitset however, I could not achive to put T values in the place of x. Thank you so much
@learner999,

Why don't you just give the ORIGINAL problem - not your remarkably confused paraphrasing of it.

https://xyproblem.info/
Thank you @lastchance. Actually, this is the original problem. In a part of my project, for each time period( t=1...T) , the decision variable can be binary combinatio of a given number N. I thought, in the first place , I should generate vectors but after thinking a liitle more, I have realized that what I need is decision variables as I shown in the previous explanation. Because I need to make some mathematical operations in the further steps of the projects via these decision variables like ; int K=V[1][0][1]- V[1][1][0].

Thank you so much
Now I'm completely mystified!
No, you haven't defined the original requirement. All you're stated is your interpretation of how you think to undertake a part of the original.
goodness... what about...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
  int t;
  t = 2;
  int p = pow(2,t);
  bitset<3> bitz; //I still have NO clue how you know you want 3 bits. but here are 3 bits. 
  unsigned int r;
  for(int i = 0; i < p; i++)
  {
	r = i;
	bitz = r;
	cout << bitz[0] << bitz[1] << bitz[2] << "|" << bitz << endl; //see how can use as a
// container or just print it directly into a string or whatever?
//interestingly, its stored backwards: see the left hand bits in the output are reversed...
  }
}



C:\c>a
000|000
100|001
010|010
110|011



that is the mechanics of the code you want.
if you had x = 1, just add this line:
bitz[2] = x; //assert x = 0 or 1!

you can make a vector of bitset<3> type to store all those values. but at some point, its getting silly. They are just tiny integers with iota values.
Last edited on
learner999 wrote:
the decision variable can be binary combination of a given number N


You aren't confusing "binary combination" with "bitwise operation" are you?
Hello @lastchance and @jonnin . Thank you for your comments. As I am quite beginner, I might have difficulties to explain the problem. To be honest, I do not know bitwise operation. Here is the problem. We have two machines (it might be three machihes though, any given number) . The availability of the machines are defined by binary values : 0 and 1. There might be 4 different combinations for the availability of these two machines . Which are
00
01
10 // first machine is available the second is not.
11

We need to compute the cost of these different cases for 3 weeks as follows:
G[1][0][0]=100; this is known initially, first indice represents first week, second and third ones are the first member of combinations above , now we will compute the following combinations:

G[1][0][1]= G[1][0][0] + if the difference between the last indices of G vectors (1-0=1) is equal to 1, then add 10 , otherwise do not add anything.
G[1][1][0]= G[1][0][1] + if the difference between the last indices of G vectors (0-1=-1) is equal to 1, then add 10 , otherwise do not add anything.
G[1][1][1]= G[1][0][0]+ if the difference between the last indices of G vectors (1-0=0) is equal to 1, then add 10 , otherwise do not add anything.

I will go on computing the same G arrays when t =2
G[2][0][0]= G[1][1][1]+..... the same condition
G[2][0][1]= G[1][1][0]+..... the same condition
G[2][1][0]= G[1][1][0]+..... the same condition
G[2][1][1]= G[1][0][1]+..... the same condition

To compute G[2][][], I will use the results of G[1][][]. The problem for me is not sum or condition part. The problem how to define those
G[1][0][0]
G[1][0][1]
G[1][1][0]
G[1][1][1],

G[2][0][0]
G[2][0][1]
G[2][1][0]
G[2][1][1],

G[3][0][0]
G[3][0][1]
G[3][1][0]
G[3][1][1] and the correspinding values to them after calculation?

If we have three machines then we will need
G[1][0][0][0]
G[1][0][1][1]
....

G[1][1][1][1]
for t=2;
G[2][0][0][0]
G[2][0][1][1]
....

G[2][1][1][1]

for t=3;
G[3][0][0][0]
G[3][0][1][1]
....// Other binary combination of 3.

G[3][1][1][1]


I hope it is a little more clear. I am on this task for nearly one week. I appreciate all the helps. Thank you so much;



1
2
3
4
5
6
7
8
using u32 = std::uint32_t;
u32 constexpr avail_mask = 0b0001111; // 4 bits of availability information, enough for 4 machines
u32 constexpr week_mask  = 0b1110000; // 3 bits of time information, enough for 2^3 = 8 weeks
u32 constexpr size = (avail_mask | week_mask) + 1;
static u32 data[size];

// The cost for week w with availability a is accessed using cost_for(w, a)
u32& cost_for(u32 week, u32 avail) { return data[(week << 4) | avail]; }
Last edited on
Registered users can post here. Sign in or register to post.