A challenge for all newbies

Please support our Computer Science advertiser: Learn about neural networks and artificial intelligence.
Reply

Join Date: Jun 2005
Posts: 2,055
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: A challenge for all newbies

 
0
  #21
Jul 8th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 181
Reputation: Paul.Esson is an unknown quantity at this point 
Solved Threads: 10
Paul.Esson's Avatar
Paul.Esson Paul.Esson is offline Offline
Junior Poster

Re: A challenge for all newbies

 
0
  #22
Jul 14th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,849
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: A challenge for all newbies

 
0
  #23
Jul 14th, 2005
>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;
}
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: A challenge for all newbies

 
0
  #24
Jul 14th, 2005
>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
*/
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,849
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: A challenge for all newbies

 
0
  #25
Jul 14th, 2005
>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;
}
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 138
Reputation: a1eio is an unknown quantity at this point 
Solved Threads: 21
a1eio's Avatar
a1eio a1eio is offline Offline
Junior Poster

Re: A challenge for all newbies

 
0
  #26
Aug 9th, 2005
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'
>>>
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,055
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: A challenge for all newbies

 
0
  #27
Aug 9th, 2005
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>
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 24
Reputation: numerouno is an unknown quantity at this point 
Solved Threads: 0
numerouno's Avatar
numerouno numerouno is offline Offline
Newbie Poster

Re: A challenge for all newbies

 
0
  #28
Aug 9th, 2005
STOP! My brain is hurting!
Career Diagnostics

http://www.careerdiagnostics.com
Home of "Best Foot Forward!" - a "how to" guide to writing your Resume or CV
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 19
Reputation: egoleo is an unknown quantity at this point 
Solved Threads: 0
egoleo egoleo is offline Offline
Newbie Poster

Re: A challenge for all newbies

 
0
  #29
Aug 11th, 2005
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:
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 80
Reputation: Daishi is an unknown quantity at this point 
Solved Threads: 2
Daishi Daishi is offline Offline
Junior Poster in Training

Re: A challenge for all newbies

 
0
  #30
Aug 17th, 2005
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Computer Science Forum


Views: 8363 | Replies: 29
Thread Tools Search this Thread



Tag cloud for Computer Science
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC