When you want to left or right shift some bits...or splice some addresses together like below..So when would you need this skill? Hardware programming comes to mind..

#include <stdio.h>
#include <stdlib.h>

unsigned long two = 3345;

int main(int argc, char**argv)
{
	unsigned long one = 1234;
	void *addr = (void*)NULL;

	fprintf(stdout, "one addr->%p\n", (void*)&one);
	fprintf(stdout, "two addr->%p\n", (void*)&two);

	/*splice the first 12 bits of two onto the first 12 bits of one*/
	/*and save the result in addr*/

	addr = (void*)(((unsigned long)&one>>12<<12) + ((unsigned long)&two<<52>>52));	

	fprintf(stdout, "result addr->%p\n", addr);
	exit(EXIT_SUCCESS);
}

This was written quickly...I hope its correct..

Edited 6 Years Ago by gerard4143: n/a

Comments
ridiculously confusing.

This was written quickly...I hope its correct..

Would it have taken long to give the code a quick test?

And there is no reason to call exit() every time you need to stop a program. A simple return will exit the program from main()

Would it have taken long to give the code a quick test?

And there is no reason to call exit() every time you need to stop a program. A simple return will exit the program from main()

It was written/run once and appeared to work...that's quickly in my books.

It was written/run once and appeared to work...that's quickly in my books.

what constitutes "appearing to work" for you? not having the computer crash? if so, then yeah, it "appeared to work".

other than that, it was full of compiler warnings and gives nonsensical output.

Boo.

what constitutes "appearing to work" for you? not having the computer crash? if so, then yeah, it "appeared to work".

other than that, it was full of compiler warnings and gives nonsensical output.

Boo.

Your probably running a 32 bit machine...

This article has been dead for over six months. Start a new discussion instead.