Could someone explain where and when you would use << and >>. I read these 2 articles but don't really understand them.
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B
http://en.wikipedia.org/wiki/Arithmetic_shift

Recommended Answers

All 5 Replies

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..

commented: ridiculously confusing. -1

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...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.