943,519 Members | Top Members by Rank

Ad:
You are currently viewing page 3 of this multi-page discussion thread; Jump to the first page
Jul 8th, 2005
0

Re: A challenge for all newbies

To swap Perl scalars without writing a third variable:
($x, $y) = ($y, $x);

If you want to swap Perl arrays, well, you'd be better off having used array references and swapping those like above.
Team Colleague
Reputation Points: 1133
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Jul 14th, 2005
0

Re: A challenge for all newbies

Interesting how this has been done in two different ways, one with exclusive or operator and one with addition and subtraction.

In java we could also use, XOR in the same way.

int a, b;

a ^= b;
b ^= a;
a ^= b;

Ok, so I have a little challange now. Bit of fun.

Today we are going to swap the nibbles around in a byte
so the byte 11101010 will turn into 10101110.

can use as many varibles as you wish, but there must be a varible at the end that contains the swapped byte.
Reputation Points: 21
Solved Threads: 10
Junior Poster
Paul.Esson is offline Offline
181 posts
since Feb 2005
Jul 14th, 2005
0

Re: A challenge for all newbies

>Today we are going to swap the nibbles around in a byte
Pfft, too easy. Do the shuffle: shift left shift right shift left OR shift right:
#include <stdio.h>
#include <limits.h>

#define HEXIT ( CHAR_BIT / 2 )

unsigned char swap_nibble ( unsigned char x )
{
  return x << HEXIT >> HEXIT << HEXIT | x >> HEXIT;
}

int main ( void )
{
  unsigned char x = 0xEA;

  printf ( "Before: 0x%X\n", x );
  printf ( "After:  0x%X\n", swap_nibble ( x ) );

  return 0;
}
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 14th, 2005
0

Re: A challenge for all newbies

>Do the shuffle: shift left shift right shift left OR shift right:

I'm too white and can't dance as well:
#include <stdio.h>
#include <limits.h>

#define HEXIT ( CHAR_BIT / 2 )

unsigned char swap_nibble ( unsigned char x )
{
  return x << HEXIT | x >> HEXIT;
}

int main ( void )
{
  unsigned char x = 0xEA;

  printf ( "Before: 0x%X\n", x );
  printf ( "After:  0x%X\n", swap_nibble ( x ) );

  return 0;
}

/* my output
Before: 0xEA
After:  0xAE
*/
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jul 14th, 2005
0

Re: A challenge for all newbies

>I'm too white and can't dance as well:
It's easy, just shake it, shake it, shake it. Then shake it, shake it, shake it again:
#include <stdio.h>
#include <limits.h>

#define HEXIT ( CHAR_BIT / 2 )

unsigned char swap_nibble ( unsigned char x )
{
  return x << HEXIT >> HEXIT << HEXIT >> HEXIT << HEXIT >> HEXIT << HEXIT
    | x >> HEXIT << HEXIT >> HEXIT << HEXIT >> HEXIT << HEXIT >> HEXIT;
}

int main ( void )
{
  unsigned char x = 0xEA;

  printf ( "Before: 0x%X\n", x );
  printf ( "After:  0x%X\n", swap_nibble ( x ) );

  return 0;
}
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Aug 9th, 2005
0

Re: A challenge for all newbies

just use clean python:
>>> a = 5
>>> b = 4
>>> a, b = b, a
>>> a
4
>>> b
5
>>> a = 'text1'
>>> b = 'text2'
>>> a, b = b, a
>>> a
'text2'
>>> b
'text1'
>>>
Reputation Points: 26
Solved Threads: 24
Junior Poster
a1eio is offline Offline
140 posts
since Aug 2005
Aug 9th, 2005
0

Re: A challenge for all newbies

Bah, just use clean, erm, XSLT.

<xsl:template name="blah">
    <xsl:param name="x" select="2"/>
    <xsl:param name="y" select="5"/>
    <xsl:param name="swapped" select="0"/>

    ...

    <xsl:choose>
        <xsl:when test="$swapped = 0">
            <!-- First we run this code. -->

            <!-- And say we want to swap x and y! -->

            <!-- Oh no, we can't change their values! Curse!  Recurse!!-->
            <xsl:call-template name="blah">
                <xsl:with-param name="x" select="$y"/>
                <xsl:with-param name="y" select="$x"/>
                <xsl:with-param name="swapped" select="1"/>
            </xsl:call-template>
        </xsl:when>

        <!-- In the spirit of XML, they picked the most verbose name
        out of the choices 'else', 'default', and 'otherwise.' -->
        <xsl:otherwise>
            <!-- Now x and y are swapped! -->

            ...
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
Team Colleague
Reputation Points: 1133
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Aug 9th, 2005
0

Re: A challenge for all newbies

STOP! My brain is hurting!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
numerouno is offline Offline
24 posts
since Apr 2005
Aug 11th, 2005
0

Re: A challenge for all newbies

yeah. its really a great challenge for us. Just look at the way guys are sending in those codes. Man i am kinda of confused now. :mrgreen:
Reputation Points: 10
Solved Threads: 0
Newbie Poster
egoleo is offline Offline
19 posts
since Jul 2005
Aug 17th, 2005
0

Re: A challenge for all newbies

Here we go. I decided to do it without using any of those dirty bitwise operators. It was fun. :o No, I don't actually code like this.

#include <iostream>
#include <string>

using namespace std;

string printBinary(unsigned char aByte) {  //  prints aByte in binary.
  int moder=128;
  string ret;
  for (int i=0; i<8; i++) {
    if (aByte >= moder) {
      ret+='1';
      aByte-=moder;
    } else
      ret+='0';
    moder /= 2;
  }
  return ret;
}

unsigned char getFromBinary(string buffer) {
  unsigned char ret=0, moder=1;
  for (int i=buffer.length()-1; i>=0; i--) {
    if ((char)buffer[i] == '1')
      ret+=moder;
    moder*=2;
  }
  return ret;
}

unsigned char swapBit(unsigned char byte, int a, int b) {
  string str = printBinary(byte);
  char temp = str[a];
  str[a] = str[b];
  str[b] = temp;

  return getFromBinary(str);
}

int main(int argc, char **argv) {
  unsigned char a = getFromBinary("11101010");
  cout << "A is " <<  (int)a << "\t" << printBinary(a) << endl;

  a = swapBit(a, 4, 5);
  cout << "A is " << (int)a << "\t" << printBinary(a) << endl;

  a = swapBit(a, 2, 3);
  cout << "A is " << (int)a << "\t" << printBinary(a) << endl;

  a = swapBit(a, 3, 4);
  cout << "A is " << (int)a << "\t" << printBinary(a) << endl;

  a = swapBit(a, 1, 2);
  cout << "A is " << (int)a << "\t" << printBinary(a) << endl;

  return 0;
}

It's horrible, I know.

-Fredric
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Daishi is offline Offline
80 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Computer Science Forum Timeline: Pls...click me
Next Thread in Computer Science Forum Timeline: concurrent service





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC