Hi! i want to spilt the 16bit word into two 8bit words, is there any key words for it. i use the p89V51 mcu and c language Cx51 keil compiler ver3.
regards, K.T.Venkatesan.
Standard ANSI 'C' - nothing specifically to do with Keil or the 8051:
1. Portable: use the bitwise shift & mask operators to separate the low & high 8 bits;
2. Non-Portable: use a union of one 16-bit type and two 8-bit types;
3. Non-Portable: Use a pointer to point at the low & high bytes.
The 2nd two are non-portable because they rely upon the byte ordering used by the specific compiler;
The union is likely to be the fastest.
Hi! is that only way shifting for split the two bytes from a 16bit word? there is no more key words in the ansi c, am i right.
-K.T.V
No, of course shifting is not the only way - I gave three ways to do it, didn't I?!
"there is no more key words in the ansi c, am i right."
There are no keywords at all specifically for this purpose.
If you don't want shift and/or mask you can also use division and modulus
e.g.,
int Value; char Hi; char Lo; Value = 0x1234; Hi = Value/0x100; Lo = Value%0x100;
Ah yes - that makes four!
Of course, division by a power of two is equivalent to shifting.
I can't see any advantage of division & modulus over shift & mask - and there is the distinct possibility that it will be grossly inefficient if the compiler does actually implement it as the maths functions...
Hi! that's nice to use this method.Thank you matt and Neil.
Hi! sorry! u gave me three ways! but i thing the shifting is the most easy way.
The most "easy" way for the processor is to use the union - as this involves zero runtime processing.
The other ways may or may not result in some runtime processing, depending on how smart the compiler and/or optimiser is.
Whether any particular one is "easier" in terms of developer effort, maintenance, etc could be a subject for a very long and drawn-out discussion...
I'll start:
"the union ... involves zero runtime processing."
Apart from any possible overheads due to unaligned/misaligned access on some processors...?
"I can't see any advantage of division & modulus over shift & mask ..."
Right-shifting an int having a negative value is implementation-defined, whereas div/mod are not.
in C extern the16 extern hi8 ext6ern lo8
in assembler the16: ds 0 hi8: ds 1 lo8: ds 1
this is breaking every 'rule' that exist but does allow simple addressing of all 3 entities.
Now that we list methods, this one should be included.
Erik
View all questions in Keil forum