Fastest Method
I beleive int is the size of the system word; so in the case of 16-bit systems sizeof(int)==2, 32 bit systems sizeof(int)==4, and 64 bit systems sizeof(int)==8.
sizeof(char), I think, is always 1. For wide chars (wchar_t) the size is 2 bytes.
MBCS use two chars to represent one character, however sizeof(char) is still 1.
quote:
The only thing you are guaranteed is that:
sizeof(char) == 1
sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)
This is correct.... and remember it well everyone. I started working on a new platform, porting some existing code, and got very unexpected results because sizeof(long) changed from 32-bits to 64-bits.
But because I had used a typedef''d type everywhere, it was easy to change only the typedef to fix it all.
(I haven''t tested it yet, but I''m guessing (long long) on my platform is 128-bit.)
---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!
It says in my trusty Soustrup book:
1 == sizeof( char ) <= sizeof( short ) <= sizeof( int ) <= sizeof( long ).
Unfortunately, that 1 does NOT have any units. Sizeof simply returns sizes in multiple of char!
Therefore, it is NOT guaranteed that a char is one byte.
It is only guaranteed ( through another spec ) that char is AT LEAST 8 bits, a short at least 16, and a long at least 32.
People might not remember what you said, or what you did, but they will always remember how you made them feel.
~ (V)^|) |<é!t|-| ~
1 == sizeof( char ) <= sizeof( short ) <= sizeof( int ) <= sizeof( long ).
Unfortunately, that 1 does NOT have any units. Sizeof simply returns sizes in multiple of char!
Therefore, it is NOT guaranteed that a char is one byte.
It is only guaranteed ( through another spec ) that char is AT LEAST 8 bits, a short at least 16, and a long at least 32.
People might not remember what you said, or what you did, but they will always remember how you made them feel.
~ (V)^|) |<é!t|-| ~
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
September 19, 2000 07:47 AM
you could go:
and declare you''r 32bit thingy to be a 32bit then just move the bytes to their correct positions with simple assignments
union 32bit{unsinged long MyVal;struct{ unsigned char Byte1; unsigned char Byte2; unsigned char Byte3; unsigned char Byte4;}}
and declare you''r 32bit thingy to be a 32bit then just move the bytes to their correct positions with simple assignments
September 19, 2000 07:50 AM
note that using unions this way is way faster than any shift operations will ever be!
Be sure that you put the values in the right places, intel boxes have a weird way of storing words and dwords... but used correctly it works.
Be sure that you put the values in the right places, intel boxes have a weird way of storing words and dwords... but used correctly it works.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement