if(size & 1)
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)
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
im beginning to understand the value of understanding binary...
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)
"Work hard and it might happen."
-XSquared
Just out of curiosity, will it be the same on big-endian machines?Originally posted by Stoned_Coder
Code:if (size_is_odd)
The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.
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.
Away.
Isn't endian-ness transparent as long as you don't use pointers?
Even for shifts?
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
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 ); }
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Endianness is also an issue when reading binary data files and the like.
That's what my code was for - reading PNG files...
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Thanks. Thats what I thought, I wasn't quite sure though.Originally posted by blackrat364
yeah it would, because 1 would be stored like this in binary:
0001 0000
so it would still work
The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.
Big endian/little endian doesn't alter the layout of a byte, it affects the layout of multiple bytes forming words.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.
Last edited by Cat; 06-18-2003 at 02:04 PM.
Just assume those numbers are each a byte - the logic still holds. Even though it is a little endian setup.
Last edited by Zach L.; 06-18-2003 at 01:45 PM.
The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.
The logic does, but it might be confusing.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
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
The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.