how is this evaluated?

• 06-17-2003
jriano
how is this evaluated?
if(size & 1)
• 06-17-2003
Zach L.
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.
• 06-17-2003
Stoned_Coder
Code:

`if (size_is_odd)`
• 06-17-2003
JaWiB
im beginning to understand the value of understanding binary...
• 06-17-2003
Zach L.
Quote:

Originally posted by Stoned_Coder
Code:

`if (size_is_odd)`

Just out of curiosity, will it be the same on big-endian machines?
• 06-18-2003
confuted
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.
• 06-18-2003
CornedBee
Isn't endian-ness transparent as long as you don't use pointers?
Even for shifts?
• 06-18-2003
CornedBee
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      ); }```
• 06-18-2003
Cat
Endianness is also an issue when reading binary data files and the like.
• 06-18-2003
CornedBee
That's what my code was for - reading PNG files...
• 06-18-2003
Zach L.
Quote:

Originally posted by blackrat364
yeah it would, because 1 would be stored like this in binary:

0001 0000

so it would still work

Thanks. Thats what I thought, I wasn't quite sure though.
• 06-18-2003
Cat
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.

Big endian/little endian doesn't alter the layout of a byte, it affects the layout of multiple bytes forming words.

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.
• 06-18-2003
Zach L.
Just assume those numbers are each a byte - the logic still holds. Even though it is a little endian setup.
• 06-18-2003
CornedBee
The logic does, but it might be confusing.
• 06-18-2003
Zach L.
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```