<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Getting portions of number back</title>
		<link>http://www.allegro.cc/forums/view/604447</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sat, 26 Jun 2010 16:11:49 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Still learning bit manipulations, so sorry for not understanding...My question is how do I get my &quot;packed&quot; data back from using something like:
</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="k1">enum</span> CardColor
<span class="number">  2</span><span class="k2">{</span>
<span class="number">  3</span>  CardColor_Red,
<span class="number">  4</span>  CardColor_Blue,
<span class="number">  5</span>  CardColor_Green,
<span class="number">  6</span>  CardColor_Yellow,
<span class="number">  7</span><span class="k2">}</span><span class="k2">;</span>
<span class="number">  8</span><span class="k1">enum</span> CardType
<span class="number">  9</span><span class="k2">{</span>
<span class="number"> 10</span>  CardType_Zero,
<span class="number"> 11</span>  CardType_One,
<span class="number"> 12</span>  CardType_Two,
<span class="number"> 13</span>  CardType_Three,
<span class="number"> 14</span>  CardType_Four,
<span class="number"> 15</span>  CardType_Five,
<span class="number"> 16</span>  CardType_Six,
<span class="number"> 17</span>  CardType_Seven,
<span class="number"> 18</span>  CardType_Eight,
<span class="number"> 19</span>  CardType_Nine,
<span class="number"> 20</span><span class="k2">}</span><span class="k2">;</span>
<span class="number"> 21</span><span class="c">///////////////////////////////////////////////////////////////////////////////</span>
<span class="number"> 22</span><span class="k1">int</span> main<span class="k2">(</span> <span class="k1">void</span> <span class="k2">)</span>
<span class="number"> 23</span><span class="k2">{</span>
<span class="number"> 24</span>  <span class="k1">int</span> card <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>
<span class="number"> 25</span>  <span class="k1">char</span> cardColor <span class="k3">=</span> CardColor_Blue<span class="k2">;</span>
<span class="number"> 26</span>  <span class="k1">char</span> cardType <span class="k3">=</span> CardType_Seven<span class="k2">;</span>
<span class="number"> 27</span>  <span class="k1">char</span> showFace <span class="k3">=</span> <span class="n">1</span><span class="k2">;</span>
<span class="number"> 28</span>  <span class="k1">char</span> reserved <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>
<span class="number"> 29</span>  card <span class="k3">=</span> <span class="k2">(</span><span class="k2">(</span>cardColor<span class="k3">&lt;</span><span class="k3">&lt;</span><span class="n">24</span><span class="k2">)</span><span class="k3">|</span><span class="k2">(</span>cardType<span class="k3">&lt;</span><span class="k3">&lt;</span><span class="n">16</span><span class="k2">)</span><span class="k3">|</span><span class="k2">(</span>showFace<span class="k3">&lt;</span><span class="k3">&lt;</span><span class="n">8</span><span class="k2">)</span><span class="k3">|</span><span class="k2">(</span>reserved<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 30</span>  <span class="c">// now how do I extract the cardColor, cardType, and showFace values back from card?! :(</span>
<span class="number"> 31</span>  <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span>
<span class="number"> 32</span><span class="k2">}</span>
<span class="number"> 33</span><span class="c">///////////////////////////////////////////////////////////////////////////////</span>
</div></div><p>
My question is now how do I extract the cardColor, cardType, showFace, and reserved values back from the variable card?! <img src="http://www.allegro.cc/forums/smileys/sad.gif" alt=":(" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Don Freeman)</author>
		<pubDate>Thu, 24 Jun 2010 22:52:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>cardface = (card &gt;&gt; 8) &amp; 0xFF;<br />cardcolor = (card &gt;&gt; 24) &amp; 0xFF:</p><p>wouldn&#39;t that work?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Arthur Kalliokoski)</author>
		<pubDate>Thu, 24 Jun 2010 22:55:41 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks for the fast response! I got it to work with:
</p><div class="source-code snippet"><div class="inner"><pre>  <span class="k1">char</span> reserved <span class="k3">=</span> <span class="k2">(</span>card <span class="k3">&gt;</span><span class="k3">&gt;</span> <span class="n">0</span> <span class="k2">)</span> <span class="k3">&amp;</span> <span class="n">0xFF</span><span class="k2">;</span>
  <span class="k1">char</span> showFace <span class="k3">=</span> <span class="k2">(</span>card <span class="k3">&gt;</span><span class="k3">&gt;</span> <span class="n">8</span> <span class="k2">)</span> <span class="k3">&amp;</span> <span class="n">0xFF</span><span class="k2">;</span>  
  <span class="k1">char</span> value <span class="k3">=</span> <span class="k2">(</span> card <span class="k3">&gt;</span><span class="k3">&gt;</span> <span class="n">16</span> <span class="k2">)</span> <span class="k3">&amp;</span> <span class="n">0xFF</span><span class="k2">;</span>
  <span class="k1">char</span> color <span class="k3">=</span> <span class="k2">(</span> card <span class="k3">&gt;</span><span class="k3">&gt;</span> <span class="n">24</span> <span class="k2">)</span> <span class="k3">&amp;</span> <span class="n">0xFF</span><span class="k2">;</span>
</pre></div></div><p>

If you don&#39;t care, what is the &amp;0xFF for? I don&#39;t know why this stuff confuses me at times. <img src="http://www.allegro.cc/forums/smileys/sad.gif" alt=":(" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Don Freeman)</author>
		<pubDate>Thu, 24 Jun 2010 23:09:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The 0xFF masks off the stuff &quot;above&quot; the desired value, i.e. cardface = (card &gt;&gt; 8) would still have cardcolor in it.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Arthur Kalliokoski)</author>
		<pubDate>Thu, 24 Jun 2010 23:11:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Oh, okay! Thanks a million! <img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=":D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Don Freeman)</author>
		<pubDate>Thu, 24 Jun 2010 23:12:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/604447/872142#target">Don Freeman</a> said:</div><div class="quote"><p>If you don&#39;t care, what is the &amp;0xFF for? I don&#39;t know why this stuff confuses me at times. <img src="http://www.allegro.cc/forums/smileys/sad.gif" alt=":(" /></p></div></div><p>

<span class="source-code"><span class="k3">&amp;</span> <span class="n">0xFF</span></span> =&gt; &quot;Truncate to only the last 8 bits&quot;.</p><p>1 AND 1 == 1<br />1 AND 0 == 0<br />0 AND 1 == 0<br />0 AND 0 == 0</p><p>So say you have 0x5559DF50 &amp; 0xFF. The AND is done bit-for-bit, so
</p><pre>01010101010110011101111101011001
(0x5559DF59)
&amp;
00000000000000000000000011111111
(0xFF)
=
00000000000000000000000001011001
(0x00000059)</pre><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gnolam)</author>
		<pubDate>Thu, 24 Jun 2010 23:18:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Is it considered clean to use an enum like that? I mean, it&#39;s practically deprecated in C++. Perhaps any of you code gods or monkeys could enlighten me please.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (weapon_S)</author>
		<pubDate>Fri, 25 Jun 2010 02:09:21 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It might not be entirely clean, but it is handy. And enums will always cast down to ints.</p><p>In one of my projects I use them for bit flags, which means I can&#39;t actually use the enum type to store the final values, so any place the enum is used, I just pass ints around.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Fri, 25 Jun 2010 02:14:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Enums are deprecated in C++? WTF?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (BAF)</author>
		<pubDate>Fri, 25 Jun 2010 02:38:06 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Only on Tuesdays.  Since us indie developers code on weekends and take Tuesday off, we never heard about it.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Fri, 25 Jun 2010 10:17:49 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Treating it like a list of int&#39;s that starts at 0 is... isn&#39;t it?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (weapon_S)</author>
		<pubDate>Fri, 25 Jun 2010 11:12:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Not to my knowledge.<br />It is true though that enums and ints are different types in C++, and converting between them is subject to the same rules as every other cast.<br />Since an enums underlying type is int, every possible value of any given enum can be converted to int without loss of precision. The reverse is not true, though, because most enums do not define values for the entire possible range of integers (and in fact, they never can, because the size of an integer may vary between platforms and is usually larger than the minimum size mandated by the language standard). Hence, casting from enum to int is a widening cast, which may be done implicitly, but casting from int to enum is narrowing and needs to be explicit. It can also fail, and you must be prepared to handle (or prevent) this.<br />Consequently, the following operations are all OK:<br />- assigning enum to int<br />- comparing enum vs. int<br />- using enum as array index<br />- passing enum as int argument<br />- using enum as case label in a switch<br />- performing bit-wise logic with enums, as long as the result is assigned to an int, not an enum<br />Which covers most, but not all, cases that may arise when you use an enum to define integer constants.</p><p>An enum is still tremendously handy for a list of integer constants, mainly because:
</p><ul><li><p>it is evident at a glance that the constants belong together
</p></li><li><p>by using the enum type for function arguments, you can force only valid values to be passed, even if you later assign them to an int internally
</p></li><li><p>you don&#39;t need to keep track of which values are used for what (although you can), because enums auto-number by default
</p></li></ul></div>]]>
		</description>
		<author>no-reply@allegro.cc (Tobias Dammers)</author>
		<pubDate>Fri, 25 Jun 2010 14:07:10 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/604447/872300#target">Tobias Dammers</a> said:</div><div class="quote"><p>
It can also fail, and you must be prepared to handle (or prevent) this.</p></div></div><p>
</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/604447/872300#target">Tobias Dammers</a> said:</div><div class="quote"><p>
</p><ul><li><p>by using the enum type for function arguments, you can force only valid values to be passed, even if you later assign them to an int internally
</p></li></ul></div></div><p>
<img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" />
</p><pre class="terminal scroll">bamccaig@castopulence:~/src$ cat main.cpp
#include &lt;iostream&gt;

enum Example
{
    ZERO,
    ONE,
    TWO
};

void printExample(Example);

int main(int argc, char * argv[])
{
    Example e;
    unsigned long long l = -1;

    std::cout &lt;&lt; &quot;Example{&quot;
              &lt;&lt; ZERO &lt;&lt; &quot;, &quot;
              &lt;&lt; ONE &lt;&lt; &quot;, &quot;
              &lt;&lt; TWO
              &lt;&lt; &quot;}&quot;
              &lt;&lt; std::endl;

    std::cout &lt;&lt; &quot;l=&quot; &lt;&lt; l &lt;&lt; std::endl;

    e = (Example)5;

    printExample(e);

    e = (Example)l;

    printExample(e);

    printExample((Example)0xdeadbeef);

    return 0;
}

void printExample(Example e)
{
    std::cout &lt;&lt; &quot;e=&quot; &lt;&lt; e &lt;&lt; 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$</pre><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Fri, 25 Jun 2010 21:39:15 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hmmm... OK, suppose that first point is moot.<br />The second point I should probably rephrase as &quot;by using the enum type for function arguments, you can strongly suggest a range of valid values to the using code, even if you later assign them to an int internally&quot;. While you <i>can</i> pass integer values into a function through an enum argument, you need to use an explicit cast to do so, and, assuming you have at least half a brain, this should flash an alarm bell and tell you that it&#39;s probably a very good idea to first check if the cast is actually valid, or better yet, if you shouldn&#39;t be storing the value as the correct enum type in the first place.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Tobias Dammers)</author>
		<pubDate>Sat, 26 Jun 2010 16:11:49 +0000</pubDate>
	</item>
</rss>
