<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>operator++(bool)</title>
		<link>http://www.allegro.cc/forums/view/615840</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sat, 07 Nov 2015 06:18:41 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Did you know this was valid C++
</p><div class="source-code snippet"><div class="inner"><pre><span class="p">#include &lt;iostream&gt;</span>
<span class="k1">using</span> std::cout<span class="k2">;</span>
<span class="k1">using</span> std::endl<span class="k2">;</span>

<span class="k1">int</span> main <span class="k2">(</span><span class="k2">)</span>
<span class="k2">{</span>
  <span class="k1">bool</span> b <span class="k3">=</span> <span class="k1">false</span><span class="k2">;</span>
  cout <span class="k3">&lt;</span><span class="k3">&lt;</span> b <span class="k3">&lt;</span><span class="k3">&lt;</span> endl<span class="k2">;</span>
  b<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span>
  cout <span class="k3">&lt;</span><span class="k3">&lt;</span> b <span class="k3">&lt;</span><span class="k3">&lt;</span> endl<span class="k2">;</span>
  b<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span>
  cout <span class="k3">&lt;</span><span class="k3">&lt;</span> b <span class="k3">&lt;</span><span class="k3">&lt;</span> endl<span class="k2">;</span>
  <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
You can call increment on <span class="source-code"><span class="k1">false</span></span> to get <span class="source-code"><span class="k1">true</span></span>, and increment on <span class="source-code"><span class="k1">true</span></span> to get ... <span class="source-code"><span class="k1">true</span></span>.<br />And if you replace ++ with -- , that&#39;s not valid C++.</p><p>I only found this out just today; it&#39;s been slated to be deprecated in C++17 (<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0002r0.html">link</a>) Enjoy while you can!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Peter Hull)</author>
		<pubDate>Tue, 27 Oct 2015 00:25:38 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>WHAT? I thought for sure (bool)++ was negate. This is like Javascript-level stupidity.</p><p><div class="media-player youtube"><div style="margin: 1em 2em; background: url(/images/movie.png); width: 180px; height: 100px; text-align: center;"><a href="http://www.allegro.cc//www.youtube.com/watch?v=20BySC_6HyY" target="_blank"><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/object/5/1/518354fd8304b75674a0c4a6cf96e613.jpg" border="0" alt="video" title="Click to play video" /></a></div></div></p><p>Is that compiler dependent?</p><p>[edit] Also, side note: Lots of people I know are surprised that you can multiply strings in some (read: good) languages. It just means repeat. Which is insanely useful when you need to do something like:
</p><div class="source-code snippet"><div class="inner"><pre>print <span class="s">"text header\n"</span> <span class="k3">+</span> <span class="s">"-"</span><span class="k3">*</span><span class="n">80</span> <span class="k3">+</span> <span class="s">"\n"</span>
</pre></div></div><p>

As well as other formatting I can&#39;t recall off the top my head. I love learning about stuff like that where you go &quot;Where has this been all my life?&quot;
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Katko)</author>
		<pubDate>Tue, 27 Oct 2015 01:42:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Is <span class="source-code">b<span class="k3">+</span><span class="k3">+</span></span> just the same as <span class="source-code">b <span class="k3">=</span> <span class="k1">true</span></span>? Anyone tested it or looked at the assembly?</p><p>By the way, negation would be <span class="source-code">b ^<span class="k3">=</span> <span class="n">1</span></span>. Unless C++ disallows it, of course <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (torhu)</author>
		<pubDate>Tue, 27 Oct 2015 01:57:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><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="p">#include &lt;stdio.h&gt;</span>
<span class="number">  2</span><span class="k1">int</span> main<span class="k2">(</span><span class="k2">)</span>
<span class="number">  3</span><span class="k2">{</span>
<span class="number">  4</span>  <span class="k1">bool</span> a <span class="k3">=</span> <span class="k1">false</span><span class="k2">;</span>
<span class="number">  5</span>  <span class="k1">bool</span> b <span class="k3">=</span> <span class="k1">true</span><span class="k2">;</span>
<span class="number">  6</span>  <a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span><span class="s">"a = %i\n"</span>, a<span class="k2">)</span><span class="k2">;</span>
<span class="number">  7</span>  <a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span><span class="s">"b = %i\n"</span>, b<span class="k2">)</span><span class="k2">;</span>
<span class="number">  8</span>  
<span class="number">  9</span>  <a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span><span class="s">"a++ = %i\n"</span>, <span class="k2">(</span>a<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span> <span class="k2">)</span><span class="k2">;</span>
<span class="number"> 10</span>  <a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span><span class="s">"b++ = %i\n"</span>, <span class="k2">(</span>b<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span> <span class="k2">)</span><span class="k2">;</span>
<span class="number"> 11</span>  
<span class="number"> 12</span>  a <span class="k3">=</span> <span class="k1">false</span><span class="k2">;</span>
<span class="number"> 13</span>  b <span class="k3">=</span> <span class="k1">true</span><span class="k2">;</span>  
<span class="number"> 14</span>  <a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span><span class="s">"++a = %i\n"</span>, <span class="k2">(</span><span class="k3">+</span><span class="k3">+</span>a<span class="k2">)</span> <span class="k2">)</span><span class="k2">;</span>
<span class="number"> 15</span>  <a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span><span class="s">"++b = %i\n"</span>, <span class="k2">(</span><span class="k3">+</span><span class="k3">+</span>b<span class="k2">)</span> <span class="k2">)</span><span class="k2">;</span>
<span class="number"> 16</span>  
<span class="number"> 17</span>  a <span class="k3">=</span> <span class="k1">false</span><span class="k2">;</span>
<span class="number"> 18</span>  b <span class="k3">=</span> <span class="k1">true</span><span class="k2">;</span>
<span class="number"> 19</span>  <a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span><span class="s">"!a = %i\n"</span>, <span class="k3">!</span>a<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 20</span>  <a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span><span class="s">"!b = %i\n"</span>, <span class="k3">!</span>b<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 21</span>  
<span class="number"> 22</span>  a <span class="k3">=</span> <span class="k1">false</span><span class="k2">;</span>
<span class="number"> 23</span>  b <span class="k3">=</span> <span class="k1">true</span><span class="k2">;</span>
<span class="number"> 24</span>  <a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span><span class="s">"a^1 = %i\n"</span>, a^<span class="n">1</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 25</span>  <a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span><span class="s">"b^1 = %i\n"</span>, b^<span class="n">1</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 26</span>
<span class="number"> 27</span>
<span class="number"> 28</span>  <span class="c">//lol.</span>
<span class="number"> 29</span>  a <span class="k3">=</span> <span class="k1">false</span><span class="k2">;</span>
<span class="number"> 30</span>  b <span class="k3">=</span> <span class="k1">true</span><span class="k2">;</span>
<span class="number"> 31</span>  <a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span><span class="s">"(1 - (bool)a*-1) = %i\n"</span>, <span class="n">1</span> <span class="k3">-</span> <span class="k2">(</span><span class="k1">bool</span><span class="k2">)</span><span class="k2">(</span>a<span class="k3">*</span><span class="k3">-</span><span class="n">1</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 32</span>  <a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span><span class="s">"(1 - (bool)b*-1) = %i\n"</span>, <span class="n">1</span> <span class="k3">-</span> <span class="k2">(</span><span class="k1">bool</span><span class="k2">)</span><span class="k2">(</span>b<span class="k3">*</span><span class="k3">-</span><span class="n">1</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 33</span>  <span class="k2">}</span>
</div></div><p>
</p><div class="source-code snippet"><div class="inner"><pre>a <span class="k3">=</span> <span class="n">0</span>
b <span class="k3">=</span> <span class="n">1</span>
a<span class="k3">+</span><span class="k3">+</span> <span class="k3">=</span> <span class="n">0</span> <span class="c">//nope</span>
b<span class="k3">+</span><span class="k3">+</span> <span class="k3">=</span> <span class="n">1</span> <span class="c">//nope</span>
<span class="k3">+</span><span class="k3">+</span>a <span class="k3">=</span> <span class="n">1</span> <span class="c">//nope</span>
<span class="k3">+</span><span class="k3">+</span>b <span class="k3">=</span> <span class="n">1</span> <span class="c">//nope</span>
<span class="k3">!</span>a <span class="k3">=</span> <span class="n">1</span> <span class="c">//yes</span>
<span class="k3">!</span>b <span class="k3">=</span> <span class="n">0</span> <span class="c">//yes</span>
a^<span class="n">1</span> <span class="k3">=</span> <span class="n">1</span> <span class="c">//yes</span>
b^<span class="n">1</span> <span class="k3">=</span> <span class="n">0</span> <span class="c">//yes</span>
<span class="n">1</span> <span class="k3">-</span> <span class="k2">(</span><span class="k1">bool</span><span class="k2">)</span>a<span class="k3">*</span><span class="k3">-</span><span class="n">1</span> <span class="k3">=</span> <span class="n">1</span> <span class="c">//yes</span>
<span class="n">1</span> <span class="k3">-</span> <span class="k2">(</span><span class="k1">bool</span><span class="k2">)</span>b<span class="k3">*</span><span class="k3">-</span><span class="n">1</span> <span class="k3">=</span> <span class="n">0</span> <span class="c">//yes... lols</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Katko)</author>
		<pubDate>Tue, 27 Oct 2015 02:11:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>With the postfix operators you have to increment and print them in two different statements, otherwise you get the original value <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /></p><p>But it looks like bools are limited to be either 0 or 1. I guess <span class="source-code">b <span class="k3">+</span> <span class="n">1</span></span> would be 1?</p><p>By the way, what compiler version did you use?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (torhu)</author>
		<pubDate>Tue, 27 Oct 2015 03:28:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>What possible reason would you have to increment a bool anyway?  Either set it to true or false, anything else is madness.  Using ischar() as a number base makes about as much sense.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Arthur Kalliokoski)</author>
		<pubDate>Tue, 27 Oct 2015 05:11:04 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well, looking at it from C it makes sense - there bool is the same as int and anything except zero means true. So if x is 1 and you do x++ it gets 2, which is still true.</p><p>It is stupid that they allowed ++ for bool in C++, but so are 1000 other things in C++, so I don&#39;t see anything special here <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Tue, 27 Oct 2015 07:18:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I forgot to mention I&#39;m running GCC. I don&#39;t recall the version, it&#39;s in the newer (maybe experimental?) branch which is necessary for proper c++14 (or whatever) support. I don&#39;t recall which, it&#39;s been a long time since I had to install it.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Katko)</author>
		<pubDate>Tue, 27 Oct 2015 08:18:44 +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/615840/1017484#target">Elias</a> said:</div><div class="quote"><p>Well, looking at it from C it makes sense </p></div></div><p>Why not x-- then?</p><p>As it is, you can flip false to true, or true to true, but not true to false.  I would think the operators should be symmetrical, or not at all.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Mark Oates)</author>
		<pubDate>Tue, 27 Oct 2015 08:23:09 +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/615840/1017484#target">Elias</a> said:</div><div class="quote"><p> So if x is 1 and you do x++ it gets 2, which is still true.</p></div></div><p>But <i>why</i> would you do that with a bool, as opposed to some sort of stack pointer or ring buffer?  Not to mention the wraparound, which is admittedly huge.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Arthur Kalliokoski)</author>
		<pubDate>Tue, 27 Oct 2015 09:05:24 +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/615840/1017480#target">torhu</a> said:</div><div class="quote"><p> By the way, what compiler version did you use? 
</p></div></div><p>I used MSVC, clang and gcc (not sure of the latest versions but all recent)</p><p>I suppose if you view bool as a one-bit arithmetic type then incrementing true is an overflow and that results in undefined behaviour - so the value could be anything.</p><p>And I suppose if you incremented something N times and decremented it N times you might expect to be back where you started, which can&#39;t be guaranteed for bool so they disabled &#39;--&#39;.</p><p>Maybe there was some reason for it back when they wrote the spec and maybe it just slipped through the net?</p><p>[edit] The fact that it&#39;s <b>post</b> increment gives it one possible use, though it&#39;s not hard to find alternatives:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">void</span> printlist<span class="k2">(</span><span class="k1">int</span> n<span class="k2">)</span> <span class="k2">{</span>
 <span class="k1">bool</span> comma <span class="k3">=</span> <span class="k1">false</span><span class="k2">;</span>
 <span class="k1">for</span> <span class="k2">(</span><span class="k1">int</span> i <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> i <span class="k3">&lt;</span> n<span class="k2">;</span> <span class="k3">+</span><span class="k3">+</span>i<span class="k2">)</span> <span class="k2">{</span>
  <span class="k1">if</span> <span class="k2">(</span>comma<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span> <span class="k2">{</span>
   cout <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">','</span><span class="k2">;</span>
  <span class="k2">}</span>
  cout <span class="k3">&lt;</span><span class="k3">&lt;</span> i<span class="k2">;</span>
 <span class="k2">}</span>
<span class="k2">}</span>
</pre></div></div><p>
The original example was <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3668">here</a> using C++ ranged for statement which makes a bit more sense.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Peter Hull)</author>
		<pubDate>Tue, 27 Oct 2015 17:46:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I really don&#39;t understand why they added a separate function for just that use case, though...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (torhu)</author>
		<pubDate>Tue, 27 Oct 2015 19:19:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m of the opinion that if a sensible meaning can be defined for an operator then it should be. I like being able to make code terse and operators are the best way to achieve that from the language or a global library. I don&#39;t see this as a problem at all, as long as the results are defined. If anything, it&#39;s a WTF to me that you cannot decrement. It&#39;s no surprise to most of us that a boolean type is typically represented by a 0/^0/!0 byte or bit. And we all understand what the result would be of incrementing those. It may not entirely make sense logically, but at the same time a useful meaning can be applied to and derived from it.</p><p><img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Tue, 27 Oct 2015 21:32:09 +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/615840/1017496#target">Peter Hull</a> said:</div><div class="quote"><p>
I suppose if you view bool as a one-bit arithmetic type then incrementing true is an overflow and that results in undefined behaviour - so the value could be anything.
</p></div></div><p>
But we have tons of defined overflow scenarios. Unsigned overflows, signed overflows, float overflows. It seems strange they would just leave that one alone.</p><p>My opinion--and it&#39;s about as valuable as that--is that the more things that &quot;don&#39;t immediately make sense&quot; the more a programmer has to keep in his mind at all times while programming. And the more balls a programmer has to juggle at once, the more likely you hit create an error.</p><p>I&#39;m sure there&#39;s some brain function / sign-of-intelligence that corresponds to how many ideas a person can have in their brain at one time. And the more taken up by the programming language, the less available for comprehending the task at hand. Which is why many people solve complex problems outside of code... by solving the problem separated from implementing that problem, you free up some &quot;slots&quot; in your brain to help take a &quot;too big to fit&quot; problem and hopefully fit it.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
[edit] The fact that it&#39;s post increment gives it one possible use, though it&#39;s not hard to find alternatives:
</p></div></div><p>
Actually, have you tested for an error in that code? What happens if you increment a bool 65536 times? Does it eventually wrap a 8/16/32/64-bit integer around to zero?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Katko)</author>
		<pubDate>Tue, 27 Oct 2015 22:05:56 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I compiled this as C++ in VS 2015, no optimizations:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">void</span> incbool<span class="k2">(</span><span class="k1">bool</span> b<span class="k2">)</span>
<span class="k2">{</span>
  b<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

And got this:
</p><div class="source-code snippet"><div class="inner"><pre>?incbool@@YAX_N@Z <span class="k2">(</span><span class="k1">void</span> __cdecl incbool<span class="k2">(</span><span class="k1">bool</span><span class="k2">)</span><span class="k2">)</span><span class="k2">:</span>
  <span class="n">00000000</span><span class="k2">:</span> <span class="n">55</span>                 push        ebp
  <span class="n">00000001</span><span class="k2">:</span> <span class="n">8B</span> EC              mov         ebp,esp
  <span class="n">00000003</span><span class="k2">:</span> <span class="n">51</span>                 push        ecx
  <span class="n">00000004</span><span class="k2">:</span> <span class="n">8A</span> <span class="n">45</span> <span class="n">08</span>           mov         al,byte ptr <span class="k2">[</span>ebp<span class="k3">+</span><span class="n">8</span><span class="k2">]</span>
  <span class="n">00000007</span><span class="k2">:</span> <span class="n">88</span> <span class="n">45</span> FF           mov         byte ptr <span class="k2">[</span>ebp-1<span class="k2">]</span>,al
  <span class="n">0000000A</span><span class="k2">:</span> C6 <span class="n">45</span> <span class="n">08</span> <span class="n">01</span>        mov         byte ptr <span class="k2">[</span>ebp<span class="k3">+</span><span class="n">8</span><span class="k2">]</span>,<span class="n">1</span>
  <span class="n">0000000E</span><span class="k2">:</span> <span class="n">8B</span> E5              mov         esp,ebp
  <span class="n">00000010</span><span class="k2">:</span> <span class="n">5D</span>                 pop         ebp
  <span class="n">00000011</span><span class="k2">:</span> C3                 ret
</pre></div></div><p>

It&#39;s just copied, and then set to true.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (torhu)</author>
		<pubDate>Wed, 28 Oct 2015 00:56:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>All 4 of gcc, clang, g++, clang++ produce the same asm output for C/C++ code (simply moving the value 1 into the variable). So actually C&#39;s ++ operator behaves the same on a bool as C++&#39;s.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Wed, 28 Oct 2015 01:25:06 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yeah, I get get same result in VS 2015. Only the name mangling of the function changes, the assembly is otherwise identical. I guess VS finally supports C99 bool, then.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (torhu)</author>
		<pubDate>Wed, 28 Oct 2015 01:49:34 +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/615840/1017484#target">Elias</a> said:</div><div class="quote"><p>
Well, looking at it from C it makes sense - there bool is the same as int and anything except zero means true. So if x is 1 and you do x++ it gets 2, which is still true.
</p></div></div><p>
And this, boys and girls, is why you should never use bitwise operators when you should use logical operators.</p><p>Case in point:</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="number">  2</span><span class="k1">int</span> bitmask1 <span class="k3">=</span> <span class="n">0x02</span><span class="k2">;</span>
<span class="number">  3</span><span class="k1">int</span> bitmask2 <span class="k3">=</span> <span class="n">0x38</span><span class="k2">;</span>
<span class="number">  4</span>
<span class="number">  5</span><span class="k1">bool</span> somefunc1<span class="k2">(</span><span class="k1">int</span> flags<span class="k2">)</span>
<span class="number">  6</span><span class="k2">{</span>
<span class="number">  7</span>  <span class="k1">return</span> flags <span class="k3">&amp;</span> bitmask1<span class="k2">;</span>
<span class="number">  8</span><span class="k2">}</span>
<span class="number">  9</span>
<span class="number"> 10</span><span class="k1">bool</span> somefunc2<span class="k2">(</span><span class="k1">int</span> flags<span class="k2">)</span>
<span class="number"> 11</span><span class="k2">{</span>
<span class="number"> 12</span>  <span class="k1">return</span> flags <span class="k3">&amp;</span> bitmask2<span class="k2">;</span>
<span class="number"> 13</span><span class="k2">}</span>
<span class="number"> 14</span>
<span class="number"> 15</span><span class="k1">void</span> blah<span class="k2">(</span><span class="k1">int</span> someflags<span class="k2">)</span>
<span class="number"> 16</span><span class="k2">{</span>
<span class="number"> 17</span>  <span class="k1">if</span><span class="k2">(</span>somefunc1<span class="k2">(</span>someflags<span class="k2">)</span> <span class="k3">&amp;</span> somefunc2<span class="k2">(</span>someflags<span class="k2">)</span><span class="k2">)</span>
<span class="number"> 18</span>  <span class="k2">{</span>
<span class="number"> 19</span>    <span class="c">// bitwise AND</span>
<span class="number"> 20</span>    <span class="c">// As long as bitmask1 and bitmask2 don't share any flags, this will never be true.</span>
<span class="number"> 21</span>  <span class="k2">}</span>
<span class="number"> 22</span>  
<span class="number"> 23</span>  <span class="k1">if</span><span class="k2">(</span>somefunc1<span class="k2">(</span>someflags<span class="k2">)</span> <span class="k3">&amp;</span><span class="k3">&amp;</span> somefunc2<span class="k2">(</span>someflags<span class="k2">)</span><span class="k2">)</span>
<span class="number"> 24</span>  <span class="k2">{</span>
<span class="number"> 25</span>    <span class="c">// Logical AND</span>
<span class="number"> 26</span>    <span class="c">// This will be true if someflags contains at least one flag in bitmask1 and one flag in bitmask2.</span>
<span class="number"> 27</span>    <span class="c">// But in cases where we know all bools will have the same numeric value for 'true', this will be slower than a bitwise AND.</span>
<span class="number"> 28</span>  <span class="k2">}</span>
<span class="number"> 29</span><span class="k2">}</span>
</div></div><p>

Here, if bools are treated like ints, the two if()s will behave differently. However, you could create a <tt>StrictBool</tt> class that behaves like a bool should. All operators will be overloaded with versions that use C++&#39;s &#39;explicit&#39; keyword with StrictBool-paramaters to prevent implicit casting, and there will be non-explicit versions of the operators that for each paramater, regardless of it&#39;s type, do something like <tt>value = (paramater?true:false)</tt> . In this case, both if()&#39;s in the above code will be equivalent if the functions return StrictBools.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Andrei Ellman)</author>
		<pubDate>Wed, 28 Oct 2015 03:49:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yeah <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" /></p><p><span class="remote-thumbnail"><span class="json">{"name":"636x460design_01.jpg","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/4\/8\/48bed991ae72aac26032315a4ba6c62b.jpg","w":636,"h":460,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/4\/8\/48bed991ae72aac26032315a4ba6c62b"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/4/8/48bed991ae72aac26032315a4ba6c62b-240.jpg" alt="636x460design_01.jpg" width="240" height="173" /></span>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (torhu)</author>
		<pubDate>Wed, 28 Oct 2015 04:07:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I ran some rudimentary (uncomprehensive) tests for performance between either several <span class="source-code"><span class="k1">bool</span></span> types or bitwise on an int.</p><p>Bitwise was a bit slower.</p><p>...</p><p><img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=":D" /></p><div class="spoiler"><p>No, but actually it was a bit slower. Only by about ~7% or something, though. </p></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Mark Oates)</author>
		<pubDate>Wed, 28 Oct 2015 04:12:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You should be careful with that kind of thing. Many simple operations require only one cycle on the CPU to do. And if you are comparing ints to bools on a PC (32 or 64 bits), you are probably comparing 32-bit types to 8-bit types. Which one is faster depends on what you do with. You could be comparing an operation to one that&#39;s different in either implementation, purpose, or both. Just saying <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (torhu)</author>
		<pubDate>Wed, 28 Oct 2015 04:27:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Isn&#39;t it the case that the compiler ensures that bools always have a 0 or a 1 stored in their memory location?</p><p>I think Visual Studio warns if an int is implicitly cast to bool, and the wording of the warning suggests that there will be a performance hit from the operation of constraining its value.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Bruce Perry)</author>
		<pubDate>Wed, 28 Oct 2015 04:53:32 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Since ++ just copies it and sets it to true, then there&#39;s no reason not to include -- to copy and set to false.</p><p>Note: my <i>actual</i> position is that the operators should not be used at all on bool.</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/615840/1017528#target">torhu</a> said:</div><div class="quote"><p>You should be careful with that kind of thing. Many simple operations require only one cycle on the CPU to do. And if you are comparing ints to bools on a PC (32 or 64 bits), you are probably comparing 32-bit types to 8-bit types. Which one is faster depends on what you do with. You could be comparing an operation to one that&#39;s different in either implementation, purpose, or both. Just saying <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /> </p></div></div><p>All of that&#39;s probably true <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Mark Oates)</author>
		<pubDate>Wed, 28 Oct 2015 04:57:22 +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/615840/1017531#target">Mark Oates</a> said:</div><div class="quote"><p> Since ++ just copies it and sets it to true, then there&#39;s no reason not to include -- to copy and set to false.</p></div></div><p>It doesn&#39;t even need to copy, just set.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Arthur Kalliokoski)</author>
		<pubDate>Wed, 28 Oct 2015 06:13:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It has to copy, that&#39;s the whole point of postincrement.</p><p><span class="cuss"><span>Fuck</span></span> Matthew Leverton, just <span class="cuss"><span>fuck</span></span> him.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (torhu)</author>
		<pubDate>Wed, 28 Oct 2015 06:20:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Write out your truth table and all possible outcomes (4 of them).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Arthur Kalliokoski)</author>
		<pubDate>Wed, 28 Oct 2015 13:13:40 +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/615840/1017533#target">torhu</a> said:</div><div class="quote"><p>
<span class="cuss"><span>Fuck</span></span> Matthew Leverton, just <span class="cuss"><span>fuck</span></span> him.
</p></div></div><p>
Bad thread ?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (GullRaDriel)</author>
		<pubDate>Wed, 28 Oct 2015 18:52:26 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Arthur, the operator needs to return the old value of the variable from before it was set to true. For example:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">bool</span> munge<span class="k2">(</span><span class="k1">bool</span> <span class="k3">&amp;</span>x<span class="k2">)</span> <span class="k2">{</span>
    <span class="k1">return</span> x<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
That function will set x, but it will also return x&#39;s old value. To do that, it has to copy it.</p><p>Granted the compiler could optimise that away if the x++ appears as a statement with its value unused.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Bruce Perry)</author>
		<pubDate>Sun, 01 Nov 2015 04:39:57 +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/615840/1017636#target">Bruce Perry</a> said:</div><div class="quote"><p>
That function will set x, but it will also return x&#39;s old value. To do that, it has to copy it.
</p></div></div><p>
Now, if x is some large class with an overloaded &#39;++&#39; operator, ++x and x++ can make all the difference, as copying would take longer.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Andrei Ellman)</author>
		<pubDate>Fri, 06 Nov 2015 05:57:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><span class="remote-thumbnail"><span class="json">{"name":"609921","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/8\/a\/8a20a909009bfcd5bfee645bece22df7.png","w":474,"h":474,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/8\/a\/8a20a909009bfcd5bfee645bece22df7"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/8/a/8a20a909009bfcd5bfee645bece22df7-240.jpg" alt="609921" width="240" height="240" /></span>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Johan Halmén)</author>
		<pubDate>Sat, 07 Nov 2015 03:47:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=":D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Sat, 07 Nov 2015 06:18:41 +0000</pubDate>
	</item>
</rss>
