bitset in union

Programming, for all ages and all languages.
Post Reply
User avatar
Zacariaz
Member
Member
Posts: 1069
Joined: Tue May 22, 2007 2:36 pm
Contact:

bitset in union

Post by Zacariaz »

Code: Select all

static union {
  unsigned int UI[2];
  std::bitset<64> BS;
};
'std::bitset<64u> <anonymous union>::BS' with constructor not allowed in union

I dont really "need" to do this, but it enoys the crud outa me that i cant.

Is there some sort of workaround?


I guess this (in theory) would do, more or less, the same:

Code: Select all

static union {
  unsigned int UI[2];
  bool BS[64];
};
however, i have had some issues with the bool type in the past where i experienced it taking up more space that a single bit, is this the case here, and if so, how would it affect the workings of the union?
User avatar
Zacariaz
Member
Member
Posts: 1069
Joined: Tue May 22, 2007 2:36 pm
Contact:

Post by Zacariaz »

just testet the last piece of code:

Code: Select all

#include <iostream>
#include <climits>
static union {
  unsigned int UI[2];
  bool BS[64];
};

int main() {
  UI[0] = UINT_MAX;
  UI[1] = UINT_MAX;
  for(int i = 0; i < 64; i++)
    std::cout << BS[i];
  std::cin.get();
}
and it definently doesnt work. It give a weird result:
255255255255 255255255255 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000b

and it of course should be:
0xFFFFFFFFFFFFFFFF
Aali
Member
Member
Posts: 58
Joined: Sat Apr 14, 2007 12:13 pm

Post by Aali »

1. using a class in a union really makes no sense at all

2. bool is usually whatever-size-suits-the-compiler-best
and that is usually not one bit

3. you're probably better off using macros (or inline functions) to manipulate bits
User avatar
Zacariaz
Member
Member
Posts: 1069
Joined: Tue May 22, 2007 2:36 pm
Contact:

Post by Zacariaz »

ok, as i said, i dont really "need" this, but it would be very convinient for my purpose.

The thing is i have this bitset<64>, well, actually alot of them, and i need to set some specific bits. If i fx. wanted to set the 32 most significant bit, this would (in thery) be easy done by: Bs = 0xFFFFFFFF00000000ll, however, even though the compiler doesnt complain about it, it still doesnt work, in this maner it will only manipulate the lower 32 bits.

When all is said, it is not important, i just wanted to know if there was something there could be done. anyhow, i have a little code to show, not very complicated, but somewhat messy.

Code: Select all

#include <bitset>
class T {
    enum {P, B, N, R, Q, K};
    std::bitset<64> White[6], Black[6];
public:
  T() {
    for(int i = 1; i < 64; i += 8)
      White[P][i] = 1;
    for(int i = 6; i < 64; i += 8)
      Black[P][i] = 1;
    White[R][0] = 1;
    White[R][56] = 1;
    Black[R][7] = 1;
    Black[R][63] = 1;
    White[N][8] = 1;
    White[N][48] = 1;
    Black[N][15] = 1;
    Black[N][55] = 1;
    White[B][16] = 1;
    White[B][40] = 1;
    Black[B][23] = 1;
    Black[B][47] = 1;
    White[Q][24] = 1;
    Black[Q][31] = 1;
    White[K][32] = 1;
    Black[K][39] = 1;
  }
  ~T() {}
} BitBoard;
I for one doesnt like the way this is looking. I probably could make it look a little better by turning the bitboard 90 degrees, but not much, and i dont wanna do that.
User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

Post by os64dev »

see Bitfield in the following yopic

http://www.osdev.org/phpBB2/viewtopic.p ... t=bitfield
Author of COBOS
Post Reply