Pergunta

I have multiple bitsets

bitset<32> addr;
bitset<64> wdata;

and I want to put them into another bitset, essentially concatenating them into a larger bitset.

bitset<96> datain;

is there someway of doing the following

datain[95 downto 64] = addr;
datain[63 downto 0] = wdata;

I can work out how to assign single bits, but writing a loop to assign single bits seems excessive.

Thanks.

Foi útil?

Solução 2

I would definitively stick to writing the loop which makes the bit-by-bit assignment. Factor it out as a utility function (not hard at all to write). This is because std::bitset does NOT fulfill the Container concept, and thus cannot be sliced or copied using standard algorithms (like std::copy, for example).

Also, notice that because of the implementation you cannot simply get an address to a specific bit (not such a thing, though you can get a "proxy reference").

Just write it (your custom "bit slicer") and forget forever.

Just a sample of how it could look like (it's ugly but it works):

#include<bitset>                                                                                    
#include<iostream>                                                                                  

template<size_t N1, size_t N2>                                                                      
std::bitset<N1+N2> bitwise_copy(const std::bitset<N1>& b1, const std::bitset<N2>& b2) {             
  std::bitset<N1+N2> res;                                                                           
  for(size_t k=0; k<N2; k++) {                                                                      
    res[k] = b2[k];                                                                                 
  }                                                                                                 
  for(size_t k=0; k<N1; k++) {                                                                      
    res[N2+k] = b1[k];                                                                              
  }                                                                                                 
  return std::move(res);                                                                            
}                                                                                                   

int main() {                                                                                        
  std::bitset<5> a("11111");                                                                        
  std::bitset<10> b("1010101010");                                                                  
  auto ab = bitwise_copy(a, b);                                                                     

  std::cout<<" a: "<<a<<std::endl                                                                   
           <<" b: "<<b<<std::endl                                                                   
           <<"ab: "<<ab<<std::endl;                                                                 
  return 0;                                                                                         
}                                                                                                   

When you compile using gcc 4.8.1 (with -std=c++11), the output is:

 a: 11111
 b: 1010101010
ab: 111111010101010

Outras dicas

You could do something like this:

bitset<96> datain(addr.to_string<>() + wdata.to_string<>());

It's not particularly efficient.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top