3 Replies - 3142 Views - Last Post: 19 May 2015 - 01:53 PM

Poll: Big or Little Endian? (4 member(s) have cast votes)

Which do you prefer?

  1. Big Endian (1 votes [25.00%] - View)

    Percentage of vote: 25.00%

  2. Little Endian (0 votes [0.00%])

    Percentage of vote: 0.00%

  3. Don't care (3 votes [75.00%] - View)

    Percentage of vote: 75.00%

Vote Guests cannot vote

#1 infernorthor  Icon User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Endian Converter

Post icon  Posted 17 May 2015 - 04:08 PM

Endian Convert Challenge.

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

Is This A Good Question/Topic? 3
  • +

Replies To: Endian Converter

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3466
  • View blog
  • Posts: 12,349
  • Joined: 08-August 08

Re: Endian Converter

Posted 18 May 2015 - 12:44 PM

No time to work on the challenge, but here's a quick way to determine what your system's endianness is:

Sorry about the formatting. Spoiler tags seems to have messed it up!

This post has been edited by CTphpnwb: 18 May 2015 - 12:53 PM

Was This Post Helpful? 1
  • +
  • -

#3 GWatt  Icon User is offline

  • member icon

Reputation: 305
  • View blog
  • Posts: 3,102
  • Joined: 01-December 05

Re: Endian Converter

Posted 19 May 2015 - 12:44 PM

It's not fun if you can't abuse the preprocessor.


This post has been edited by GWatt: 19 May 2015 - 05:58 PM

Was This Post Helpful? 0
  • +
  • -

#4 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1372
  • View blog
  • Posts: 7,887
  • Joined: 07-September 06

Re: Endian Converter

Posted 19 May 2015 - 01:53 PM


Hopefully I don't have any typos there. For some reason my copy/paste wasn't working so I had to manually enter it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1