if(size & 1)
Printable View
if(size & 1)
Assuming 'size' is an integral type, it is the bitwise AND, and returns a 1 for every position where the two integers both have a 1, and 0 otherwise.
That, therefore, will only return true when the right-most bit is 1.
Code:if (size_is_odd)
im beginning to understand the value of understanding binary...
Just out of curiosity, will it be the same on big-endian machines?Quote:
Originally posted by Stoned_Coder
Code:if (size_is_odd)
yeah it would, because 1 would be stored like this in binary:
0001 0000
so it would still work
edit: assuming 8 bits in a byte. I think it's a pretty safe assumption.
Isn't endian-ness transparent as long as you don't use pointers?
Even for shifts?
The tricks were what I meant with pointers.
Code:// writes a big-endian double word to a memory location
static void put_be_dword(byte *target, dword dw) {
*target++ = static_cast<byte>(dw >> 24);
*target++ = static_cast<byte>(dw >> 16);
*target++ = static_cast<byte>(dw >> 8);
*target++ = static_cast<byte>(dw );
}
Endianness is also an issue when reading binary data files and the like.
That's what my code was for - reading PNG files...
Thanks. Thats what I thought, I wasn't quite sure though.Quote:
Originally posted by blackrat364
yeah it would, because 1 would be stored like this in binary:
0001 0000
so it would still work
Big endian/little endian doesn't alter the layout of a byte, it affects the layout of multiple bytes forming words.Quote:
Originally posted by blackrat364
yeah it would, because 1 would be stored like this in binary:
0001 0000
so it would still work
edit: assuming 8 bits in a byte. I think it's a pretty safe assumption.
E.g. in hex, 8 as a 32-bit little endian (four 8-bit bytes) is "08 00 00 00", in big endian, "00 00 00 08".
By definition, you can't reorganize the bits in a byte anyway -- a byte by definition is the smallest addressable location in memory. Bits have a logical order (most significant bit .. least significant bit) but not a physical order (i.e. a bit has no address). So there IS no "order" that bits are stored in memory, there is only the logical order of the bits themselves.
On the other hand, words made of multiple bytes have a logical order (most significant byte, second most significant byte, etc.) and a physical order (addresses). Because they have both a logical and physical order, there are 2 ways to store a sequence of bytes:
In order from smallest to largest physical address, you can have:
[Most significant byte] [Second most significant byte] ... [Least Significant Byte] <= This is "big endian"
[Least significant byte] [Second least significant byte] ... [Most Significant Byte] <= This is "little endian"
"Endian"ness describes the way that bytes within words are mapped between their logical and physical orders. Each has advantages and disadvantages. Big endian is more human-readable; little-endian makes code for conversion, etc. conceptually simpler. Both are used; Motorola prefers big endian, Intel little endian.
Just assume those numbers are each a byte - the logic still holds. Even though it is a little endian setup.
The logic does, but it might be confusing.
Alrighty then... Presenting the various formats of 1 (as expressed in a 32 bit integer):
Code:memory address : 0 1 2 3
bid endian : 0x00 0x00 0x00 0x01
little endian : 0x01 0x00 0x00 0x00