This is a simple challenge to create an endian converter. This will test your knowledge of bitwise operations and conversion rules.
Endian what now?
Now, you might be asking what is endian or endianness? The common smallest atomic unit on a computer has been decided to be a byte or 8 bits, as such every byte is given a unique address. But,what happens when you want to read or write several bytes to represent a short or int for example? Well, the problem is that there is flexibility, the other was patent disputes. Anyways, there are different ways to story an array of bytes. The major two are Little Endian and Big Endian. The difference is basically left to right, or right to left, there could more ways to order bytes, but those people would be crazy.
With the two versions floating around, if programs, files, pictures are to be understood by different computers converting endianness might be a necessary step. Most people won't need to worry about these things, but it is an important thing to know that it exists and understand how to convert.
Little Endian - least significant byte occupies smallest address.
Big Endian - most significant byte occupies smallest address.
If you read addresses from smallest to largest, then Big Endian matches human readable numbers.
Example: given the hex value 0x40302010 Address Little Endian Big Endian 0x0 0x10 0x40 0x1 0x20 0x30 0x2 0x30 0x20 0x3 0x40 0x10 compact: 0x10203040 0x40302010
I'm not going to set many rules, but to give people a target, try to convert the endianess of int (4 byte).
int BigEndian(int input);
int LittleEndian(int input);
It would be good to have a scalable solution(size of type doesn't matter), and you can expand that example with more types, and templated versions,
Code for elegance, efficiency, safety, whatever matters to you.
The above deals with converting bare data.
As an additional challenges.
-Try create your own functions to handle reading and printing of numeric strings with endianness aware functions.
For example for little endian compiler;
int x = 0x124; the compiler converts the numeric string "0x124" into little endian.
-Can you figure out a way to programmatically determine the machine's endianness?
To eliminate the simplest case I will post it here. (no conversion needed)
I am interested to see all the different ways to solve the same problem.
** remember to put all code answer in code and spoiler , square bracket tags.