|
|
| Getting portions of number back |
|
Don Freeman
Member #5,110
October 2004
|
Still learning bit manipulations, so sorry for not understanding...My question is how do I get my "packed" data back from using something like: 1enum CardColor
2{
3 CardColor_Red,
4 CardColor_Blue,
5 CardColor_Green,
6 CardColor_Yellow,
7};
8enum CardType
9{
10 CardType_Zero,
11 CardType_One,
12 CardType_Two,
13 CardType_Three,
14 CardType_Four,
15 CardType_Five,
16 CardType_Six,
17 CardType_Seven,
18 CardType_Eight,
19 CardType_Nine,
20};
21///////////////////////////////////////////////////////////////////////////////
22int main( void )
23{
24 int card = 0;
25 char cardColor = CardColor_Blue;
26 char cardType = CardType_Seven;
27 char showFace = 1;
28 char reserved = 0;
29 card = ((cardColor<<24)|(cardType<<16)|(showFace<<8)|(reserved));
30 // now how do I extract the cardColor, cardType, and showFace values back from card?! :(
31 return 0;
32}
33///////////////////////////////////////////////////////////////////////////////
My question is now how do I extract the cardColor, cardType, showFace, and reserved values back from the variable card?! -- |
|
Arthur Kalliokoski
Second in Command
February 2005
|
cardface = (card >> 8) & 0xFF; wouldn't that work? They all watch too much MSNBC... they get ideas. |
|
Don Freeman
Member #5,110
October 2004
|
Thanks for the fast response! I got it to work with: char reserved = (card >> 0 ) & 0xFF; char showFace = (card >> 8 ) & 0xFF; char value = ( card >> 16 ) & 0xFF; char color = ( card >> 24 ) & 0xFF;
If you don't care, what is the &0xFF for? I don't know why this stuff confuses me at times. -- |
|
Arthur Kalliokoski
Second in Command
February 2005
|
The 0xFF masks off the stuff "above" the desired value, i.e. cardface = (card >> 8) would still have cardcolor in it. They all watch too much MSNBC... they get ideas. |
|
Don Freeman
Member #5,110
October 2004
|
Oh, okay! Thanks a million! -- |
|
gnolam
Member #2,030
March 2002
|
Don Freeman said: If you don't care, what is the &0xFF for? I don't know why this stuff confuses me at times. & 0xFF => "Truncate to only the last 8 bits". 1 AND 1 == 1 So say you have 0x5559DF50 & 0xFF. The AND is done bit-for-bit, so 01010101010110011101111101011001 (0x5559DF59) & 00000000000000000000000011111111 (0xFF) = 00000000000000000000000001011001 (0x00000059)
-- |
|
weapon_S
Member #7,859
October 2006
|
Is it considered clean to use an enum like that? I mean, it's practically deprecated in C++. Perhaps any of you code gods or monkeys could enlighten me please. |
|
Thomas Fjellstrom
Member #476
June 2000
|
It might not be entirely clean, but it is handy. And enums will always cast down to ints. In one of my projects I use them for bit flags, which means I can't actually use the enum type to store the final values, so any place the enum is used, I just pass ints around. -- |
|
BAF
Member #2,981
December 2002
|
Enums are deprecated in C++? WTF? |
|
ImLeftFooted
Member #3,935
October 2003
|
Only on Tuesdays. Since us indie developers code on weekends and take Tuesday off, we never heard about it. |
|
weapon_S
Member #7,859
October 2006
|
Treating it like a list of int's that starts at 0 is... isn't it? |
|
Tobias Dammers
Member #2,604
August 2002
|
Not to my knowledge. An enum is still tremendously handy for a list of integer constants, mainly because:
--- |
|
bamccaig
Member #7,536
July 2006
|
Tobias Dammers said: It can also fail, and you must be prepared to handle (or prevent) this.
Tobias Dammers said:
bamccaig@castopulence:~/src$ cat main.cpp
#include <iostream>
enum Example
{
ZERO,
ONE,
TWO
};
void printExample(Example);
int main(int argc, char * argv[])
{
Example e;
unsigned long long l = -1;
std::cout << "Example{"
<< ZERO << ", "
<< ONE << ", "
<< TWO
<< "}"
<< std::endl;
std::cout << "l=" << l << std::endl;
e = (Example)5;
printExample(e);
e = (Example)l;
printExample(e);
printExample((Example)0xdeadbeef);
return 0;
}
void printExample(Example e)
{
std::cout << "e=" << e << std::endl;
}
bamccaig@castopulence:~/src$ g++ -Wall main.cpp
bamccaig@castopulence:~/src$ ./a.out
Example{0, 1, 2}
l=18446744073709551615
e=5
e=-1
e=-559038737
bamccaig@castopulence:~/src$
-- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
|
Tobias Dammers
Member #2,604
August 2002
|
Hmmm... OK, suppose that first point is moot. --- |
|
|