jeudi 28 mai 2015

flags as union of bitset and uint64_t

Recently learned about unions in c++ and I come up with this in order to handle multiple flags represented by a single uint64_t,etc

union myflags_t
{
  uint64_t var;
  std::bitset<64> bits;

  myflags_t() {  memset( this, 0, sizeof(uint64_t)); }
};

The alternative way I was using before was bit operations, specifically setting a bit and changing the nth bit to x.

Instead I can use

myflags_t flags;
flags.var = 0;
flags.bits[nth bit] = ...

My question is Which is faster the union or bit operations?

Also if it is faster to use the union why haven't I come across this before?

Aucun commentaire:

Enregistrer un commentaire