| | |
A challenge for all newbies
Please support our Computer Science advertiser: Learn about neural networks and artificial intelligence.
![]() |
To swap Perl scalars without writing a third variable:
If you want to swap Perl arrays, well, you'd be better off having used array references and swapping those like above.
($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.
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.
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.
>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:
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
>Do the shuffle: shift left shift right shift left OR shift right:
I'm too white and can't dance as well:
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
>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:
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
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' >>>
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> 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
http://www.careerdiagnostics.com
Home of "Best Foot Forward!" - a "how to" guide to writing your Resume or CV
•
•
Join Date: Aug 2005
Posts: 80
Reputation:
Solved Threads: 2
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.
It's horrible, I know.
-Fredric
#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
![]() |
Other Threads in the Computer Science Forum
- Previous Thread: Pls...click me
- Next Thread: concurrent service
Views: 8363 | Replies: 29
| Thread Tools | Search this Thread |
Tag cloud for Computer Science
ai algorithm algorithms amazon assignment assignmenthelp assignments battery bigbrother binary bittorrent blogging bomb business cern clueless codebreaker compiler computer computers computerscience computertrackingsoftware conversion csc data dataanalysis dataintepretation development dfa dissertation dissertations dissertationtopic ebook employment energy extensions floatingpoint foreclosure foreclosuresoftware fuel gadgets geeks givemetehcodez government hardware homeowners homework homeworkassignment homeworkhelp ibm ideas internet iphone ipod itcontracts jobs kindle laser laws lazy linkbait mainframes marketing mobileapplication msaccess nano netbeans networking news os p2p parser piracy piratebay programming rasterizer research sam-being-cute sas science security sex simulation software spoonfeeding spying sql stephenfry student study supercomputer supercomputing syntactic technology textfield tree two'scompliment uk virus ww2






