<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'How to SWAP 2 Bits in a BYTE Data' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'How to SWAP 2 Bits in a BYTE Data' posted on the 'C and C++' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2013 Programmers Heaven</copyright>
    <pubDate>Tue, 18 Jun 2013 00:55:51 -0700</pubDate>
    <lastBuildDate>Tue, 18 Jun 2013 00:55:51 -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>How to SWAP 2 Bits in a BYTE Data</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/345224/345224/how-to-swap-2-bits-in-a-byte-data/</link>
      <description>Hi Friends,&lt;br /&gt;
&lt;br /&gt;
Plz send me the code to swap two bits of byte data ,&lt;br /&gt;
Waiting for positive reply,&lt;br /&gt;
&lt;br /&gt;
Ex: Before:  0x80&lt;br /&gt;
    After :  0x01&lt;br /&gt;
&lt;br /&gt;
Rajesh&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/345224/345224/how-to-swap-2-bits-in-a-byte-data/</guid>
      <pubDate>Tue, 19 Sep 2006 04:56:46 -0700</pubDate>
      <category>C and C++</category>
    </item>
    <item>
      <title>Re: How to SWAP 2 Bits in a BYTE Data</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/345224/345225/re-how-to-swap-2-bits-in-a-byte-data/#345225</link>
      <description>: Hi Friends,&lt;br /&gt;
: &lt;br /&gt;
: Plz send me the code to swap two bits of byte data ,&lt;br /&gt;
: Waiting for positive reply,&lt;br /&gt;
: &lt;br /&gt;
: Ex: Before:  0x80&lt;br /&gt;
:     After :  0x01&lt;br /&gt;
: &lt;br /&gt;
: Rajesh&lt;br /&gt;
: &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
int main()
{
  int x = 0x80;
  x = 0x01;

  return 0;
}
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
Enjoy! And don't hesitate to beg for more code!&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/345224/345225/re-how-to-swap-2-bits-in-a-byte-data/#345225</guid>
      <pubDate>Tue, 19 Sep 2006 05:19:28 -0700</pubDate>
      <category>C and C++</category>
    </item>
    <item>
      <title>Re: How to SWAP 2 Bits in a BYTE Data</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/345224/345227/re-how-to-swap-2-bits-in-a-byte-data/#345227</link>
      <description>&lt;strong&gt;&lt;span style="color: Red;"&gt;This message was edited by Gregry2 at  2006-9-19 5:28:56&lt;/span&gt;&lt;/strong&gt;&lt;hr /&gt;&lt;br /&gt;
: Hi Friends,&lt;br /&gt;
: &lt;br /&gt;
: Plz send me the code to swap two bits of byte data ,&lt;br /&gt;
: Waiting for positive reply,&lt;br /&gt;
: &lt;br /&gt;
: Ex: Before:  0x80&lt;br /&gt;
:     After :  0x01&lt;br /&gt;
: &lt;br /&gt;
: Rajesh&lt;br /&gt;
: &lt;br /&gt;
&lt;br /&gt;
asking for code, sounds like another homework question. I'll give you pseudo code...this is off the top of my head, so it might not be efficient&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
let 'a' be the number, copy the number into 'b'

make a mask that is zeroes except on the first bit, call it mask1
make another mask that is zeroes except on the other bit, call it mask2

binary and mask1 with 'a', lets call it 'f'
binary and mask2 with 'a', lets call it 's'

if not ((f&amp;gt;0 &amp;amp;&amp;amp; s&amp;gt;0) || (f==0 &amp;amp;&amp;amp; s==0))
  if f&amp;gt;0 then b = binary or of mask2 with a
  else b= binary or of mask1 with a
return b, or just b is the value
&lt;/pre&gt;&lt;br /&gt;
not so sure if thats the best, but there&lt;br /&gt;
{2}rIng&lt;br /&gt;
&lt;br /&gt;
edit: added codetags&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/345224/345227/re-how-to-swap-2-bits-in-a-byte-data/#345227</guid>
      <pubDate>Tue, 19 Sep 2006 05:27:27 -0700</pubDate>
      <category>C and C++</category>
    </item>
    <item>
      <title>Re: How to SWAP 2 Bits in a BYTE Data</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/345224/345238/re-how-to-swap-2-bits-in-a-byte-data/#345238</link>
      <description>: Hi Friends,&lt;br /&gt;
: &lt;br /&gt;
: Plz send me the code to swap two bits of byte data ,&lt;br /&gt;
: Waiting for positive reply,&lt;br /&gt;
: &lt;br /&gt;
: Ex: Before:  0x80&lt;br /&gt;
:     After :  0x01&lt;br /&gt;
: &lt;br /&gt;
: Rajesh&lt;br /&gt;
&lt;br /&gt;
Pseudocode:&lt;br /&gt;
- Get the byte&lt;br /&gt;
- Get the two bits&lt;br /&gt;
- Swap the two bits&lt;br /&gt;
- Put the bits back in the byte&lt;br /&gt;
&lt;br /&gt;
You can do without bithsifting, but perhaps this page give some inspiration:&lt;br /&gt;
&lt;a href="http://www.codepedia.com/1/CppBitShifting"&gt;http://www.codepedia.com/1/CppBitShifting&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
See ya and good luck,&lt;br /&gt;
bilderbikkel&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/345224/345238/re-how-to-swap-2-bits-in-a-byte-data/#345238</guid>
      <pubDate>Tue, 19 Sep 2006 08:12:13 -0700</pubDate>
      <category>C and C++</category>
    </item>
    <item>
      <title>Re: How to SWAP 2 Bits in a BYTE Data</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/345224/345300/re-how-to-swap-2-bits-in-a-byte-data/#345300</link>
      <description>: : Hi Friends,&lt;br /&gt;
: : &lt;br /&gt;
: : Plz send me the code to swap two bits of byte data ,&lt;br /&gt;
: : Waiting for positive reply,&lt;br /&gt;
: : &lt;br /&gt;
: : Ex: Before:  0x80&lt;br /&gt;
: :     After :  0x01&lt;br /&gt;
: : &lt;br /&gt;
: : Rajesh&lt;br /&gt;
: &lt;br /&gt;
: Pseudocode:&lt;br /&gt;
: - Get the byte&lt;br /&gt;
: - Get the two bits&lt;br /&gt;
: - Swap the two bits&lt;br /&gt;
: - Put the bits back in the byte&lt;br /&gt;
: &lt;br /&gt;
: You can do without bithsifting, but perhaps this page give some inspiration:&lt;br /&gt;
: &lt;a href="http://www.codepedia.com/1/CppBitShifting"&gt;http://www.codepedia.com/1/CppBitShifting&lt;/a&gt;&lt;br /&gt;
: &lt;br /&gt;
: See ya and good luck,&lt;br /&gt;
: bilderbikkel&lt;br /&gt;
: &lt;br /&gt;
: &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
I don't see how you could do it without shifting in an effective way.&lt;br /&gt;
I would write it in assembler myself, since this is a typical algorithm where assembler is much more efficient - you don't have rotate and check carry in C/C++.&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/345224/345300/re-how-to-swap-2-bits-in-a-byte-data/#345300</guid>
      <pubDate>Tue, 19 Sep 2006 23:41:45 -0700</pubDate>
      <category>C and C++</category>
    </item>
    <item>
      <title>Re: How to SWAP 2 Bits in a BYTE Data</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/345224/345312/re-how-to-swap-2-bits-in-a-byte-data/#345312</link>
      <description>: : : Hi Friends,&lt;br /&gt;
: : : &lt;br /&gt;
: : : Plz send me the code to swap two bits of byte data ,&lt;br /&gt;
: : : Waiting for positive reply,&lt;br /&gt;
: : : &lt;br /&gt;
: : : Ex: Before:  0x80&lt;br /&gt;
: : :     After :  0x01&lt;br /&gt;
: : : &lt;br /&gt;
: : : Rajesh&lt;br /&gt;
: : &lt;br /&gt;
: : Pseudocode:&lt;br /&gt;
: : - Get the byte&lt;br /&gt;
: : - Get the two bits&lt;br /&gt;
: : - Swap the two bits&lt;br /&gt;
: : - Put the bits back in the byte&lt;br /&gt;
: : &lt;br /&gt;
: : You can do without bithsifting, but perhaps this page give some inspiration:&lt;br /&gt;
: : &lt;a href="http://www.codepedia.com/1/CppBitShifting"&gt;http://www.codepedia.com/1/CppBitShifting&lt;/a&gt;&lt;br /&gt;
: : &lt;br /&gt;
: : See ya and good luck,&lt;br /&gt;
: : bilderbikkel&lt;br /&gt;
: : &lt;br /&gt;
: : &lt;br /&gt;
: &lt;br /&gt;
: &lt;br /&gt;
: I don't see how you could do it without shifting in an effective way.&lt;br /&gt;
: I would write it in assembler myself, since this is a typical algorithm where assembler is much more efficient - you don't have rotate and check carry in C/C++.&lt;br /&gt;
: &lt;br /&gt;
: &lt;br /&gt;
&lt;span style="color: Blue;"&gt;Why check anything? Simply mask out the unneeded bits and then shift both values and then clear the old bit values and then OR them with the values from previoud shift.&lt;/span&gt;&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/345224/345312/re-how-to-swap-2-bits-in-a-byte-data/#345312</guid>
      <pubDate>Wed, 20 Sep 2006 04:08:14 -0700</pubDate>
      <category>C and C++</category>
    </item>
    <item>
      <title>Re: How to SWAP 2 Bits in a BYTE Data</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/345224/345318/re-how-to-swap-2-bits-in-a-byte-data/#345318</link>
      <description>: : : : Hi Friends,&lt;br /&gt;
: : : : &lt;br /&gt;
: : : : Plz send me the code to swap two bits of byte data ,&lt;br /&gt;
: : : : Waiting for positive reply,&lt;br /&gt;
: : : : &lt;br /&gt;
: : : : Ex: Before:  0x80&lt;br /&gt;
: : : :     After :  0x01&lt;br /&gt;
: : : : &lt;br /&gt;
: : : : Rajesh&lt;br /&gt;
: : : &lt;br /&gt;
: : : Pseudocode:&lt;br /&gt;
: : : - Get the byte&lt;br /&gt;
: : : - Get the two bits&lt;br /&gt;
: : : - Swap the two bits&lt;br /&gt;
: : : - Put the bits back in the byte&lt;br /&gt;
: : : &lt;br /&gt;
: : : You can do without bithsifting, but perhaps this page give some inspiration:&lt;br /&gt;
: : : &lt;a href="http://www.codepedia.com/1/CppBitShifting"&gt;http://www.codepedia.com/1/CppBitShifting&lt;/a&gt;&lt;br /&gt;
: : : &lt;br /&gt;
: : : See ya and good luck,&lt;br /&gt;
: : : bilderbikkel&lt;br /&gt;
: : : &lt;br /&gt;
: : : &lt;br /&gt;
: : &lt;br /&gt;
: : &lt;br /&gt;
: : I don't see how you could do it without shifting in an effective way.&lt;br /&gt;
: : I would write it in assembler myself, since this is a typical algorithm where assembler is much more efficient - you don't have rotate and check carry in C/C++.&lt;br /&gt;
: : &lt;br /&gt;
: : &lt;br /&gt;
: &lt;span style="color: Blue;"&gt;Why check anything? Simply mask out the unneeded bits and then shift both values and then clear the old bit values and then OR them with the values from previoud shift.&lt;/span&gt;&lt;br /&gt;
: &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
I'm assuming he wants a generic "mirror" algorithm, ie 0xAAAA would be translated to 0x5555. Otherwise the source code I first posted would do fine as well... &lt;img src="http://www.programmersheaven.com/images/Community/smile.gif" width="15" height="15" alt="" /&gt;&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/345224/345318/re-how-to-swap-2-bits-in-a-byte-data/#345318</guid>
      <pubDate>Wed, 20 Sep 2006 04:29:36 -0700</pubDate>
      <category>C and C++</category>
    </item>
    <item>
      <title>Re: How to SWAP 2 Bits in a BYTE Data</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/345224/383171/re-how-to-swap-2-bits-in-a-byte-data/#383171</link>
      <description>Hi,&lt;br /&gt;
      Plesase see the code below and make sure p2 &amp;gt; p1 for swapping bits&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
 {&lt;br /&gt;
       int n=32;&lt;br /&gt;
&lt;br /&gt;
       int p1,p2;&lt;br /&gt;
&lt;br /&gt;
       printf("\n Enter positions 1 :");&lt;br /&gt;
      scanf("%d",&amp;amp;p1);&lt;br /&gt;
&lt;br /&gt;
       printf("\n Enter positions 2 :");&lt;br /&gt;
      scanf("%d",&amp;amp;p2);&lt;br /&gt;
      n  =  ((n &amp;amp; (1&amp;lt;&amp;lt;p1)) ^ ((n&amp;amp;(1&amp;lt;&amp;lt;p2)) &amp;gt;&amp;gt; (p2-p1))  ) ? ((n ^ (1 &amp;lt;&amp;lt; p1)) ^ (1&amp;lt;&amp;lt;p2)):(n) ;&lt;br /&gt;
      printf("\n result : %d ", n);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
: Hi Friends,&lt;br /&gt;
: &lt;br /&gt;
: Plz send me the code to swap two bits of byte data ,&lt;br /&gt;
: Waiting for positive reply,&lt;br /&gt;
: &lt;br /&gt;
: Ex: Before:  0x80&lt;br /&gt;
:     After :  0x01&lt;br /&gt;
: &lt;br /&gt;
: Rajesh&lt;br /&gt;
: &lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/345224/383171/re-how-to-swap-2-bits-in-a-byte-data/#383171</guid>
      <pubDate>Tue, 16 Dec 2008 22:53:52 -0700</pubDate>
      <category>C and C++</category>
    </item>
    <item>
      <title>This post has been deleted.</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/345224/383192/this-post-has-been-deleted/#383192</link>
      <description>This post has been deleted.</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/345224/383192/this-post-has-been-deleted/#383192</guid>
      <pubDate>Wed, 17 Dec 2008 05:05:08 -0700</pubDate>
      <category>C and C++</category>
    </item>
  </channel>
</rss>