UVA – 594 – One Little, Two Little, Three Little Endians

  • Problem Statement : http://uva.onlinejudge.org/external/5/594.html
  • Type : bitwise
  • Hint : instead of traversing through bits and handling the reverse process, you can just convert the pointer to a char * and swap the 1st and the 4th and the 2nd with the 3rd. 🙂
<pre>#include<stdio.h>
using namespace std;
#define SWAP(a,b) a= a^b, b=a^b, a=a^b
int main() {
	int o, reverse;
	while (scanf("%d", &o) == 1) {
		reverse = o;
		char * bits = (char*) &reverse;
		SWAP(bits[0], bits[3]), SWAP(bits[1], bits[2]);
		printf("%d converts to %d\n", o, reverse);
	}
	return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: