Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Do we have to worry about byte orders in the process of programming a game? Do game consoles use different byte orders?

share|improve this question
1  
Are you concerned about a specific project you are working on right now or just in general? Mostly, if you are writing a game for a console, you will likely be using some well documented set of tools like XNA and input and output will be covered in its document. –  Arthur Wulf White Sep 26 '12 at 12:06
4  
If you are worried about this I really recommend Rob Pike's The byte order fallacy - trying to worry about byte order often gets you into more trouble than just ignoring it and writing truly endian-independent code. –  user744 Sep 26 '12 at 16:31
1  
It's a good read, but Rob is showing that you can get simplified reads at the expense of needing more complex writes. If you're not the one doing the writes, great! –  Kylotan Sep 26 '12 at 19:35
add comment

4 Answers

up vote 11 down vote accepted

If you're writing networking code, this often is something you have to deal with, yes.

Also it's possible that the byte ordering in a file format is not what your platform uses, so sometimes it matters there too.

share|improve this answer
add comment

Do you have to worry about it? Likely not. A vast majority of game programming is going to be at such a level that the endianness is abstracted away. Even in networking, you're almost certainly going to be using a library for networking protocols. It's good that you're aware of it, but I doubt you'd run into an issue with it.

share|improve this answer
add comment

It depends on the platforms you're targetting. For example, I believe that the PS3 is big-endien so if that's one of your targets then it's something you need to be aware of, yes.

In the home computer space the "Big 3" (Windows, Linux, Mac) are all exclusively, or all but excelusively, on Intel x86/x64 architectures these days, so endienness concerns are no longer relevant.

share|improve this answer
add comment

Most of the time, no.

Endianess is usually abstracted away in the high level modules of a game engine, and you don't have to worry about it on a daily basis. If it isn't abstracted, then the engine has a serious issue and should be fixed, because this isn't the kind of details you should be worrying about when making a game.

However if you're working on some low-level parts of a C/C++ multi-platform engine, you might have to deal with it. All three current-gen consoles use a PowerPC architecture, which is big-endian, whereas the x86 architecture used on PC is little-endian. So if you're working on some code that reads raw bytes from somewhere to put them in data structures (binary serialization, networking...), yes you will have to deal with it.

For instance, in C/C++, it's common to see this kind of byte swapping in action (not tested, welcoming corrections):

// Assume bytes comes ordered as big-endian
u16 u16FromBytes(void* data)
{
#if BIG_ENDIAN
    return (u16(data[0]) << 8) + u16(data[1]);
#else // LITTLE_ENDIAN
    return (u16(data[1]) << 8) + u16(data[0]);
#endif
}

Once again, this is OK in low-level code, but this shouldn't be something used everywhere.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.