Binarna podmiana flag

0

Czy mogę w jakiś zmyślny, binarny lub arytmetyczny sposób zamienić flagi?

#include <iostream>
using namespace std;

enum struct WindowFlags : uint32_t{
	Shown      = 1 << 0,
	FullScreen = 1 << 1,
	BorderLess = 1 << 2
};

inline WindowFlags operator|(WindowFlags a, WindowFlags b)
{return static_cast<WindowFlags>(static_cast<int>(a) | static_cast<int>(b));}

enum struct Linux_WindowFlags : uint32_t{
	FullScreen = 1 << 0,
	BorderLess = 1 << 1,
	Shown      = 1 << 3
};

uint32_t Linux_processWindowFlags(WindowFlags flags){
	uint32_t result = (uint32_t)flags;
	//TODO
	return result;
}

int main() {
	WindowFlags windowFlags = WindowFlags::Shown | WindowFlags::BorderLess;
	uint32_t linux_windowFlags = (uint32_t)Linux_processWindowFlags(windowFlags);
    if((uint32_t)linux_windowFlags & (uint32_t)Linux_WindowFlags::Shown) cout << "dobre!";
	return 0;
}

Inny przykład:

1)
xxxx
abcd

2)
xxxx
bcda

1) 1001 == 2) 0011
0

Noo.. rozpisać funkcję logiczną dla każdego bitu, albo wypisać tablice prawdy i zminimalizować je tablicą Karnaugh i złożyć bity do kupy :-)

Nie, to chyba jednak nie ma sensu :D

6

Czemu nie użyć mapy std::map<WindowFlags,Linux_WindowFlags> ?
(statyczna mapa wewnątrz funkcji konwertującej)

0

http://graphics.stanford.edu/~seander/bithacks.html#SwappingBitsXOR

w tym wypadku jednak lepiej by było stablicować to tak jak napisał @_13th_Dragon, tyle że ja bym po prostu zrobił:
uint32_t windowsToLinuxTable[8];
unit32_t linuxToWindowsTable[16];

0
struct Window{
        enum Flags : uint32_t{
            BorderLess         = 16,
            Foreign            = 2048,
            FullScreen         = 1,
            FullScreen_Desktop = 4097,
            Hidden             = 8,
            Input_Focus        = 512,
            Input_Grabbed      = 256,
            Maximized          = 128,
            Minimized          = 64,
            Mouse_Focus        = 1024,
            Resizable          = 32,
            Shown              = 4
        };
   ...
};

Tak dokładniej

0

Pytanie: na ile to ma być (super)wydajne?

Sposobów jest mnóstwo. Ot, chociażby

map<Window::Flags, MyFlags> Flags2MyFlags
{
   { Window::BorderLess, MyFlags::BorderLess },
   { Window::Foreign,    MyFlags::Foreign },
   //   etc.
};

1 użytkowników online, w tym zalogowanych: 0, gości: 1