Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How are __LITTLE_ENDIAN__ and __BIG_ENDIAN__ defined?



On Nov 15, 2005, at 4:01 PM, Dave Thorup wrote:

I've been working on creating Universal Binaries and I've been using the following where needed:

#if __LITTLE_ENDIAN__
//	do little endian stuff
#else
//	do big endian stuff
#endif

Most of this code is cross-platform C++ that is built for both Windows and Mac OS X. Since neither of these macros are defined by the VisualStudio compiler I need to come up with consistent definitions to be used on the Windows side. It was my initial understanding that both __LITTLE_ENDIAN__ and __BIG_ENDIAN__ are defined no matter if you're compiling for x86 or PPC (one is defined as 1 and the other 0).

Nope. __LITTLE_ENDIAN__ is defined and set to 1 if you're building for Intel, and __BIG_ENDIAN__ is defined and set to 1 if you're building for PowerPC. Neither is defined on the other architecture.


Also, since only one of them is defined would it be better to use #if as I did above or should I use #ifdef?

In general, I prefer #if to #ifdef just because that protects you if someone writes code like this:


#if __LITTLE_ENDIAN__
#define __BIG_ENDIAN__ 0
#endif


For what it's worth, we don't recommend using __LITTLE_ENDIAN__ or __BIG_ENDIAN__ except in very rare cases. Use the swapping functions in the system instead. They compile down to nothing when you don't need to swap and keep you from having two copies of your code.


-Eric

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/xcode-users/email@hidden

This email sent to email@hidden
References: 
 >How are __LITTLE_ENDIAN__ and __BIG_ENDIAN__ defined? (From: Dave Thorup <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.