<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'bitwise operation' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'bitwise operation' posted on the 'Beginner C/C++' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2008 Programmers Heaven</copyright>
    <pubDate>Tue, 07 Oct 2008 21:08:32 -0700</pubDate>
    <lastBuildDate>Tue, 07 Oct 2008 21:08:32 -0700</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>bitwise operation</title>
      <link>http://www.programmersheaven.com/mb/beginnercpp/371897/371897/bitwise-operation/</link>
      <description>This is an exercise out of The C Programming Language.&lt;br /&gt;
Write a function invert(x, p, n) that returns x with the n bits that begin at position p inverted (i.e., 1 changed to 0 and vice versa), leaving the others unchanged.&lt;br /&gt;
&lt;br /&gt;
I understand what they want but only have an idea of how to do it (something with the logical AND operator and one's compliment.&lt;br /&gt;
Can someone show me how to do this?&lt;br /&gt;</description>
      <pubDate>Wed, 14 May 2008 13:06:57 -0700</pubDate>
      <category>Beginner C/C++</category>
    </item>
    <item>
      <title>Re: bitwise operation</title>
      <link>http://www.programmersheaven.com/mb/beginnercpp/371897/371913/re-bitwise-operation/#371913</link>
      <description>let's try to do this. suppose x = 1010 and you need to invert the rightmost bit, or least significant bit (LSB). first you need to know what is there, 0 or 1. notice the result when you do bitwise AND with 1:&lt;br /&gt;
&lt;pre class="sourcecode"&gt;b &amp;amp; 1 = b
&lt;/pre&gt;&lt;br /&gt;
where b is a binary digit. you get the bit back whenever you AND it with 1. so to determine what is the LSB, we need to AND 1 with the LSB. the 4 bit binary of decimal 1 is:&lt;br /&gt;
&lt;pre class="sourcecode"&gt;1 = 0001&lt;/pre&gt;&lt;br /&gt;
The result:&lt;pre class="sourcecode"&gt;
x = 1010
1 = 0001
-------- (AND)
    0000
&lt;/pre&gt;&lt;br /&gt;
you get 0. so the LSB is 0. you would get a positive number if LSB was 1. to innvert this bit, you can now bitwise OR 1 to it:&lt;pre class="sourcecode"&gt;
x = 1010
1 = 0001
-------- (OR)
    1011
&lt;/pre&gt;&lt;br /&gt;
to determine the next bit, you can left shift 1 by 1 place. use the left shift operator:&lt;pre class="sourcecode"&gt;
b = 1;   //consider 4 bit integer
b &amp;lt;&amp;lt;= 1; //b now contain: 0010 in binary
&lt;/pre&gt;&lt;br /&gt;
now AND x and b:&lt;pre class="sourcecode"&gt;
x = 1010
b = 0010
-------- (AND)
    0010
&lt;/pre&gt;&lt;br /&gt;
now you get a positive number, which means the 2nd bit from right is 1. to invert it, simply apply &lt;pre class="sourcecode"&gt;x = x &amp;amp; ~b;&lt;/pre&gt;&lt;br /&gt;
beware that the result should be checked as unsigned integer. consider:&lt;pre class="sourcecode"&gt;
x = 1010
b = 1000
-------- (AND)
    1000
&lt;/pre&gt;&lt;br /&gt;
in 2's complement system (like todays most computers) the result is a negative number, unless you cast it as unsigned integer.&lt;br /&gt;
&lt;br /&gt;
learn more about &lt;a href="http://www.cprogramming.com/tutorial/bitwise_operators.html"&gt;bitwise operators&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
hope this helps.&lt;br /&gt;
&lt;hr /&gt;&lt;span style="color: Purple;"&gt;~Donotalo()&lt;/span&gt;</description>
      <pubDate>Thu, 15 May 2008 05:31:48 -0700</pubDate>
      <category>Beginner C/C++</category>
    </item>
    <item>
      <title>Re: bitwise operation</title>
      <link>http://www.programmersheaven.com/mb/beginnercpp/371897/371919/re-bitwise-operation/#371919</link>
      <description>I've read your post about a dozen times and it's helped but I still can't figure out exactly how to do this. Would this be inside a loop or will one command line do it?&lt;br /&gt;
&lt;br /&gt;
: let's try to do this. suppose x = 1010 and you need to invert the &lt;br /&gt;
: rightmost bit, or least significant bit (LSB). first you need to &lt;br /&gt;
: know what is there, 0 or 1. notice the result when you do bitwise &lt;br /&gt;
: AND with 1:&lt;br /&gt;
: &lt;pre class="sourcecode"&gt;: b &amp;amp; 1 = b
: &lt;/pre&gt;: &lt;br /&gt;
: where b is a binary digit. you get the bit back whenever you AND it &lt;br /&gt;
: with 1. so to determine what is the LSB, we need to AND 1 with the &lt;br /&gt;
: LSB. the 4 bit binary of decimal 1 is:&lt;br /&gt;
: &lt;pre class="sourcecode"&gt;: 1 = 0001&lt;/pre&gt;: &lt;br /&gt;
: The result:&lt;pre class="sourcecode"&gt;: 
: x = 1010
: 1 = 0001
: -------- (AND)
:     0000
: &lt;/pre&gt;: &lt;br /&gt;
: you get 0. so the LSB is 0. you would get a positive number if LSB &lt;br /&gt;
: was 1. to innvert this bit, you can now bitwise OR 1 to it:&lt;br /&gt;
: &lt;br /&gt;
: x = 1010&lt;br /&gt;
: 1 = 0001&lt;br /&gt;
: -------- (OR)&lt;br /&gt;
:     1011&lt;br /&gt;
: [/code]: &lt;br /&gt;
: to determine the next bit, you can left shift 1 by 1 place. use the &lt;br /&gt;
: left shift operator:&lt;br /&gt;
: &lt;br /&gt;
: b = 1;   //consider 4 bit integer&lt;br /&gt;
: b &amp;lt;&amp;lt;= 1; //b now contain: 0010 in binary&lt;br /&gt;
: [/code]: &lt;br /&gt;
: now AND x and b:&lt;pre class="sourcecode"&gt;: 
: x = 1010
: b = 0010
: -------- (AND)
:     0010
: &lt;/pre&gt;: &lt;br /&gt;
: now you get a positive number, which means the 2nd bit from right is &lt;br /&gt;
: 1. to invert it, simply apply &lt;br /&gt;
: x = x &amp;amp; ~b;[/code]: &lt;br /&gt;
: beware that the result should be checked as unsigned integer. &lt;br /&gt;
: consider:&lt;br /&gt;
: &lt;br /&gt;
: x = 1010&lt;br /&gt;
: b = 1000&lt;br /&gt;
: -------- (AND)&lt;br /&gt;
:     1000&lt;br /&gt;
: [/code]: &lt;br /&gt;
: in 2's complement system (like todays most computers) the result is &lt;br /&gt;
: a negative number, unless you cast it as unsigned integer.&lt;br /&gt;
: &lt;br /&gt;
: learn more about &lt;br /&gt;
: &lt;a href="http://www.cprogramming.com/tutorial/bitwise_operators.html"&gt;bit&lt;br /&gt;
: wise operators&lt;/a&gt;.&lt;br /&gt;
: &lt;br /&gt;
: hope this helps.&lt;br /&gt;
: &lt;hr /&gt;&lt;span style="color: Purple;"&gt;~Donotalo()&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;</description>
      <pubDate>Thu, 15 May 2008 11:51:32 -0700</pubDate>
      <category>Beginner C/C++</category>
    </item>
    <item>
      <title>Re: bitwise operation</title>
      <link>http://www.programmersheaven.com/mb/beginnercpp/371897/371925/re-bitwise-operation/#371925</link>
      <description>: I've read your post about a dozen times and it's helped but I still &lt;br /&gt;
: can't figure out exactly how to do this. Would this be inside a loop &lt;br /&gt;
: or will one command line do it?&lt;br /&gt;
&lt;br /&gt;
apparently you need a loop. suppose you need to invert bit 3 (i am assuming the LSB is bit 0) to bit 6 of an 8 bit integer x:&lt;pre class="sourcecode"&gt;
x = 1&lt;span style="color: Red;"&gt;0101&lt;/span&gt;101
&lt;/pre&gt;&lt;br /&gt;
you need to start from bit 3, so you need a 1 in an integer at position 3. you can get it by &lt;pre class="sourcecode"&gt;
b = 1 &amp;lt;&amp;lt; 3
&lt;/pre&gt;&lt;br /&gt;
now if you do&lt;pre class="sourcecode"&gt;
x &amp;amp; b
&lt;/pre&gt;&lt;br /&gt;
you can get what is at bit position 3 of x. so you can toggle it now according to my previous post. once toggled, you can left shift the 1 in b to get a 1 at bit position 4:&lt;pre class="sourcecode"&gt;
b &amp;lt;&amp;lt;= 1
&lt;/pre&gt;&lt;br /&gt;
now you can test and toggle bit position 4 of x. this way you need to continue 4 times (bit 3 to bit 6 in this example). you can use a loop to do that.&lt;br /&gt;
&lt;br /&gt;
i'm able to implement invert(x, p, n) in a single statement:&lt;pre class="sourcecode"&gt;
//inverts n bits of x from position p
int invert(int x, int p, int n) {
	return	(&amp;lt;expression&amp;gt;);
}
&lt;/pre&gt;&lt;br /&gt;
so this is your second assignment. try to do it without a loop. :)&lt;br /&gt;
&lt;hr /&gt;&lt;span style="color: Purple;"&gt;~Donotalo()&lt;/span&gt;</description>
      <pubDate>Thu, 15 May 2008 20:00:18 -0700</pubDate>
      <category>Beginner C/C++</category>
    </item>
    <item>
      <title>Re: bitwise operation</title>
      <link>http://www.programmersheaven.com/mb/beginnercpp/371897/371944/re-bitwise-operation/#371944</link>
      <description>&lt;br /&gt;
I figured out how to do the exercise with a loop but can't figure out how to do it without.&lt;br /&gt;
&lt;br /&gt;
: : I've read your post about a dozen times and it's helped but I still &lt;br /&gt;
: : can't figure out exactly how to do this. Would this be inside a loop &lt;br /&gt;
: : or will one command line do it?&lt;br /&gt;
: &lt;br /&gt;
: apparently you need a loop. suppose you need to invert bit 3 (i am &lt;br /&gt;
: assuming the LSB is bit 0) to bit 6 of an 8 bit integer x:&lt;br /&gt;
: &lt;br /&gt;
: x = 1&lt;span style="color: Red;"&gt;0101&lt;/span&gt;101&lt;br /&gt;
: [/code]: &lt;br /&gt;
: you need to start from bit 3, so you need a 1 in an integer at &lt;br /&gt;
: position 3. you can get it by &lt;br /&gt;
: &lt;br /&gt;
: b = 1 &amp;lt;&amp;lt; 3&lt;br /&gt;
: [/code]: &lt;br /&gt;
: now if you do&lt;pre class="sourcecode"&gt;: 
: x &amp;amp; b
: &lt;/pre&gt;: &lt;br /&gt;
: you can get what is at bit position 3 of x. so you can toggle it now &lt;br /&gt;
: according to my previous post. once toggled, you can left shift the &lt;br /&gt;
: 1 in b to get a 1 at bit position 4:&lt;br /&gt;
: &lt;br /&gt;
: b &amp;lt;&amp;lt;= 1&lt;br /&gt;
: [/code]: &lt;br /&gt;
: now you can test and toggle bit position 4 of x. this way you need &lt;br /&gt;
: to continue 4 times (bit 3 to bit 6 in this example). you can use a &lt;br /&gt;
: loop to do that.&lt;br /&gt;
: &lt;br /&gt;
: i'm able to implement invert(x, p, n) in a single statement:&lt;pre class="sourcecode"&gt;: 
: //inverts n bits of x from position p
: int invert(int x, int p, int n) {
: 	return	(&amp;lt;expression&amp;gt;);
: }
: &lt;/pre&gt;: &lt;br /&gt;
: so this is your second assignment. try to do it without a loop. :)&lt;br /&gt;
: &lt;hr /&gt;&lt;span style="color: Purple;"&gt;~Donotalo()&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;</description>
      <pubDate>Fri, 16 May 2008 11:26:09 -0700</pubDate>
      <category>Beginner C/C++</category>
    </item>
  </channel>
</rss>