<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: 3.8 &#8212; Bitwise operators</title>
	<atom:link href="http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/</link>
	<description></description>
	<lastBuildDate>Tue, 07 Feb 2012 12:30:29 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: panchopaz</title>
		<link>http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/comment-page-1/#comment-96587</link>
		<dc:creator>panchopaz</dc:creator>
		<pubDate>Fri, 09 Dec 2011 17:05:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/#comment-96587</guid>
		<description>Hi, excellent tutorial.

I&#039;m an electronics student and I use C to program embedded systems. Now I&#039;m looking to switch from C to C++.

In embedded systems it is really useful to have the bitwise operators. For example, bitwise operator target cpu registers and variables that control several peripheral of the cpu without altering other bits.

Also it is usefull when output pins are shared for several function in the same IO bank.

Perhaps in computers they are disappearing, but embedded systems still use them!

Save the &#124;= and &amp;= !!</description>
		<content:encoded><![CDATA[<p>Hi, excellent tutorial.</p>
<p>I&#8217;m an electronics student and I use C to program embedded systems. Now I&#8217;m looking to switch from C to C++.</p>
<p>In embedded systems it is really useful to have the bitwise operators. For example, bitwise operator target cpu registers and variables that control several peripheral of the cpu without altering other bits.</p>
<p>Also it is usefull when output pins are shared for several function in the same IO bank.</p>
<p>Perhaps in computers they are disappearing, but embedded systems still use them!</p>
<p>Save the |= and &amp;= !!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: posentel</title>
		<link>http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/comment-page-1/#comment-96193</link>
		<dc:creator>posentel</dc:creator>
		<pubDate>Tue, 25 Oct 2011 02:28:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/#comment-96193</guid>
		<description>I am a college instructor, and have taught Discrete Mathematics using C++, and have also taught video game programming using C++.  I&#039;m going through the learncpp website to pick up teaching hints and language details I may have missed.  This is the first section that I felt was poorly explained.  He should not have been explaining bitwise operators in terms of their integer representations, in general, such as changing a 4 into a 251.  That isn&#039;t how they are used.

Back in the dinosaur days of computers, memory was precious, and most real programming was done in assembly language.  Many home computers only had 16K of RAM, and multiplication wasn&#039;t built into the processor.  If you wanted to do multiplication quickly (the computers weren&#039;t that fast), you often wanted a precomputed multiplication table.  But if you needed it for 1-100, that was 10000 entries, most of the 16K.  Here&#039;s what we did: x*y = ((x+y)^2-(x-y)^2))/4.  Addition and subtraction were built into the processor, and now we only need a table of squares up to 200, so 200 entries, not 10000!  And division by 4?  Super easy, just do a right shift of 2, so &gt;&gt;2.  Just like in decimal dividing by 100 corresponds to moving the decimal 2 places, dividing by 4 corresponds to moving the &quot;decimal point&quot; 2 places in binary, and dropping the remainder.  So left and right shifts correspond to multiplying and dividing by powers of two.  With optimized compilers, this isn&#039;t as useful as it used to be.

Bitwise &amp; and &#124; are still used often for masks, especially graphically.  For example, suppose we want a flashlight effect on the screen, and only the 4 middle bits of 8 bits should be lit.  The mask 0011 1100 will turn off the bits on each side, and leave the middle 4 alone, with bitwise and:

 1101 1101
&amp;0011 1100
----------
 0001 1100

And notice what the same mask does with bitwise or:

 1101 1101
&#124;0011 1100
----------
 1111 1101

It turns the 4 middle bits on, and leaves the outer 4 alone.

What if we wanted to turn the high bit of a byte on, and leave the remaining bits alone, well then we do bitwise or with 1000 0000:

 0101 1100
&#124;1000 0000
----------
 1101 1100

This is one of the main uses of the bitwise operators.  Think how much they were used with the original black and white Mac Classic graphically.  Bitwise and would dim sections of the screen, and then bitwise or could be used to draw something else in place.  And bitwise or could also be used to draw a pattern on top of an existing picture in paint programs.</description>
		<content:encoded><![CDATA[<p>I am a college instructor, and have taught Discrete Mathematics using C++, and have also taught video game programming using C++.  I&#8217;m going through the learncpp website to pick up teaching hints and language details I may have missed.  This is the first section that I felt was poorly explained.  He should not have been explaining bitwise operators in terms of their integer representations, in general, such as changing a 4 into a 251.  That isn&#8217;t how they are used.</p>
<p>Back in the dinosaur days of computers, memory was precious, and most real programming was done in assembly language.  Many home computers only had 16K of RAM, and multiplication wasn&#8217;t built into the processor.  If you wanted to do multiplication quickly (the computers weren&#8217;t that fast), you often wanted a precomputed multiplication table.  But if you needed it for 1-100, that was 10000 entries, most of the 16K.  Here&#8217;s what we did: x*y = ((x+y)^2-(x-y)^2))/4.  Addition and subtraction were built into the processor, and now we only need a table of squares up to 200, so 200 entries, not 10000!  And division by 4?  Super easy, just do a right shift of 2, so &gt;&gt;2.  Just like in decimal dividing by 100 corresponds to moving the decimal 2 places, dividing by 4 corresponds to moving the &#8220;decimal point&#8221; 2 places in binary, and dropping the remainder.  So left and right shifts correspond to multiplying and dividing by powers of two.  With optimized compilers, this isn&#8217;t as useful as it used to be.</p>
<p>Bitwise &amp; and | are still used often for masks, especially graphically.  For example, suppose we want a flashlight effect on the screen, and only the 4 middle bits of 8 bits should be lit.  The mask 0011 1100 will turn off the bits on each side, and leave the middle 4 alone, with bitwise and:</p>
<p> 1101 1101<br />
&amp;0011 1100<br />
&#8212;&#8212;&#8212;-<br />
 0001 1100</p>
<p>And notice what the same mask does with bitwise or:</p>
<p> 1101 1101<br />
|0011 1100<br />
&#8212;&#8212;&#8212;-<br />
 1111 1101</p>
<p>It turns the 4 middle bits on, and leaves the outer 4 alone.</p>
<p>What if we wanted to turn the high bit of a byte on, and leave the remaining bits alone, well then we do bitwise or with 1000 0000:</p>
<p> 0101 1100<br />
|1000 0000<br />
&#8212;&#8212;&#8212;-<br />
 1101 1100</p>
<p>This is one of the main uses of the bitwise operators.  Think how much they were used with the original black and white Mac Classic graphically.  Bitwise and would dim sections of the screen, and then bitwise or could be used to draw something else in place.  And bitwise or could also be used to draw a pattern on top of an existing picture in paint programs.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: 3.x &#8212; Comprehensive Quiz &#171; Learn C++</title>
		<link>http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/comment-page-1/#comment-96013</link>
		<dc:creator>3.x &#8212; Comprehensive Quiz &#171; Learn C++</dc:creator>
		<pubDate>Mon, 12 Sep 2011 01:05:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/#comment-96013</guid>
		<description>[...]  3.8 &#8212; Bitwise operators      C++ Programming &#124; &#160;Print This Post   &#171; 16.4 &#8212; STL algorithms overview &#160;&#160; [...]</description>
		<content:encoded><![CDATA[<p>[...]  3.8 &#8212; Bitwise operators      C++ Programming | &nbsp;Print This Post   &laquo; 16.4 &#8212; STL algorithms overview &nbsp;&nbsp; [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Binary with C++!</title>
		<link>http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/comment-page-1/#comment-95849</link>
		<dc:creator>Binary with C++!</dc:creator>
		<pubDate>Fri, 12 Aug 2011 02:21:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/#comment-95849</guid>
		<description>[...]  [...]</description>
		<content:encoded><![CDATA[<p>[...]  [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: anas.s</title>
		<link>http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/comment-page-1/#comment-95132</link>
		<dc:creator>anas.s</dc:creator>
		<pubDate>Tue, 25 Jan 2011 03:29:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/#comment-95132</guid>
		<description>correction 

&lt;blockquote&gt;you want to write a software that would evaluate poker hands.
you could encode the hands in binary form.
every suite has 13 cards, there are 4 suites.
assign every suite 2 bytes, which give 16 bits, of which we’ll use 13bits.
each of that bit represent one of the card from A, K, Q, J, 10, 9….. (from bit12 to bit10).
to represent all 4 suites, there are 8 bytes… (32 bit)&lt;/blockquote&gt;

correction :  each of that bit represent one of the card from A, K, Q, J, 10, 9….. (from bit12 to &lt;b&gt;bit0&lt;/b&gt;).</description>
		<content:encoded><![CDATA[<p>correction </p>
<blockquote><p>you want to write a software that would evaluate poker hands.<br />
you could encode the hands in binary form.<br />
every suite has 13 cards, there are 4 suites.<br />
assign every suite 2 bytes, which give 16 bits, of which we’ll use 13bits.<br />
each of that bit represent one of the card from A, K, Q, J, 10, 9….. (from bit12 to bit10).<br />
to represent all 4 suites, there are 8 bytes… (32 bit)</p></blockquote>
<p>correction :  each of that bit represent one of the card from A, K, Q, J, 10, 9….. (from bit12 to <b>bit0</b>).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: anas.s</title>
		<link>http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/comment-page-1/#comment-95131</link>
		<dc:creator>anas.s</dc:creator>
		<pubDate>Tue, 25 Jan 2011 03:18:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/#comment-95131</guid>
		<description>to miroslav, jeff &amp; sunilla,

there&#039;s a lot of use for bitwise operation.

there might be time when you will have to deal with data represented in binary or some other kind of number base other than decimal or words (as in ASCII)

example 1,

you want to write a software that would evaluate poker hands.
you could encode the hands in binary form.
every suite has 13 cards, there are 4 suites.
assign every suite 2 bytes, which give 16 bits, of which we&#039;ll use 13bits.
each of that bit represent one of the card from A, K, Q, J, 10, 9..... (from bit12 to bit10).
to represent all 4 suites, there are 8 bytes... (32 bit)

if you represent each card in ASCII, like 7d for Seven Diamonds, it takes 2 bytes (1 byte for each ASCII character).
if you want to pass information of 10 cards, it takes you 10 x 2 bytes = 20 bytes.
where as if you use binary information it still gonna take 8 bytes.

plus, if you represent it in ASCII, you&#039;d have to do loopings to sort the hands in order from the highest value to the lowest value. if you do it in binary, you dont have to worry about the order, it&#039;s already in order.

You could also do bitwise operation on your 8-bytes representation of the cards to do comparison, or check if certain card is there or if it&#039;s a flush.... and many more.

It&#039;s an invaluable tool when you need to work smart instead of work hard.




another use is when you decide to take up microcontroller as a hobby. Like Arduino for example, where memory is still a precious commodity and speed is important.

when doing programming you will deal with logic a lot of times.....
sometime your IF statement is gonna be a very long one that it&#039;s hard to read, all entangled in nested parenthesis.
encoding them into binary representation can make it a lot easier.

Maybe if you have electronics background or at least some appreciation in digital logic design, it help you to appreciate bitwise operation more.</description>
		<content:encoded><![CDATA[<p>to miroslav, jeff &amp; sunilla,</p>
<p>there&#8217;s a lot of use for bitwise operation.</p>
<p>there might be time when you will have to deal with data represented in binary or some other kind of number base other than decimal or words (as in ASCII)</p>
<p>example 1,</p>
<p>you want to write a software that would evaluate poker hands.<br />
you could encode the hands in binary form.<br />
every suite has 13 cards, there are 4 suites.<br />
assign every suite 2 bytes, which give 16 bits, of which we&#8217;ll use 13bits.<br />
each of that bit represent one of the card from A, K, Q, J, 10, 9&#8230;.. (from bit12 to bit10).<br />
to represent all 4 suites, there are 8 bytes&#8230; (32 bit)</p>
<p>if you represent each card in ASCII, like 7d for Seven Diamonds, it takes 2 bytes (1 byte for each ASCII character).<br />
if you want to pass information of 10 cards, it takes you 10 x 2 bytes = 20 bytes.<br />
where as if you use binary information it still gonna take 8 bytes.</p>
<p>plus, if you represent it in ASCII, you&#8217;d have to do loopings to sort the hands in order from the highest value to the lowest value. if you do it in binary, you dont have to worry about the order, it&#8217;s already in order.</p>
<p>You could also do bitwise operation on your 8-bytes representation of the cards to do comparison, or check if certain card is there or if it&#8217;s a flush&#8230;. and many more.</p>
<p>It&#8217;s an invaluable tool when you need to work smart instead of work hard.</p>
<p>another use is when you decide to take up microcontroller as a hobby. Like Arduino for example, where memory is still a precious commodity and speed is important.</p>
<p>when doing programming you will deal with logic a lot of times&#8230;..<br />
sometime your IF statement is gonna be a very long one that it&#8217;s hard to read, all entangled in nested parenthesis.<br />
encoding them into binary representation can make it a lot easier.</p>
<p>Maybe if you have electronics background or at least some appreciation in digital logic design, it help you to appreciate bitwise operation more.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: MattFraust10</title>
		<link>http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/comment-page-1/#comment-87178</link>
		<dc:creator>MattFraust10</dc:creator>
		<pubDate>Fri, 25 Jun 2010 22:37:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/#comment-87178</guid>
		<description>Hey joe,

If you look back on section 3.6, the 8th comment from the top of the comments is one posted by Florian in response to the 7th comment, made by RonnieTheBear.
The post begins as stated below:

Comment by Florian
2009-12-04 13:36:39
Hey Ronnie, 

This should answer your question. There is no logical XOR in C++, but Florian gives an alternate option one could use to achieve the same ends.</description>
		<content:encoded><![CDATA[<p>Hey joe,</p>
<p>If you look back on section 3.6, the 8th comment from the top of the comments is one posted by Florian in response to the 7th comment, made by RonnieTheBear.<br />
The post begins as stated below:</p>
<p>Comment by Florian<br />
2009-12-04 13:36:39<br />
Hey Ronnie, </p>
<p>This should answer your question. There is no logical XOR in C++, but Florian gives an alternate option one could use to achieve the same ends.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: joe</title>
		<link>http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/comment-page-1/#comment-87132</link>
		<dc:creator>joe</dc:creator>
		<pubDate>Fri, 25 Jun 2010 04:30:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/#comment-87132</guid>
		<description>Is there a logical XOR operator.</description>
		<content:encoded><![CDATA[<p>Is there a logical XOR operator.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mkc</title>
		<link>http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/comment-page-1/#comment-85504</link>
		<dc:creator>Mkc</dc:creator>
		<pubDate>Fri, 28 May 2010 12:50:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/#comment-85504</guid>
		<description>I&#039;ve really enjoyed the tutorial so far, mainly because of the real life examples we&#039;ve been given along the way.

The brief elucidation at the top of this page is great but could you go a bit further and tell us why it&#039;s useful to be able to turn an 8bit 4 into a 251?! What&#039;s the practical use of these operators?

Thanks in advance and thanks for an already brilliant tutorial.

M</description>
		<content:encoded><![CDATA[<p>I&#8217;ve really enjoyed the tutorial so far, mainly because of the real life examples we&#8217;ve been given along the way.</p>
<p>The brief elucidation at the top of this page is great but could you go a bit further and tell us why it&#8217;s useful to be able to turn an 8bit 4 into a 251?! What&#8217;s the practical use of these operators?</p>
<p>Thanks in advance and thanks for an already brilliant tutorial.</p>
<p>M</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: prashant ganesha</title>
		<link>http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/comment-page-1/#comment-79973</link>
		<dc:creator>prashant ganesha</dc:creator>
		<pubDate>Sun, 07 Mar 2010 14:03:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/#comment-79973</guid>
		<description>i had read a c++ book..i dint have this kind of satisfaction after reading the book.. its truly amazing..thank you guru alex.. god bless you..</description>
		<content:encoded><![CDATA[<p>i had read a c++ book..i dint have this kind of satisfaction after reading the book.. its truly amazing..thank you guru alex.. god bless you..</p>
]]></content:encoded>
	</item>
</channel>
</rss>

