How would one go about sticking 2 WORDs into a single DWORD?
Z.
Setting two BYTES equal to a WORD
August 04, 2001 11:11 AM
A word to 2 bytes:
A dword to 2 words:
b[0]=a-((a>>8)<<8); // lowordb[1]=a>>8; // hiword
A dword to 2 words:
b[0]=a-((a>>16)<<16); // lowordb[1]=a>>16; // hiword
If maths looks ugly to u... may be u could use union .
eg.
or do a memcpy(...) from one datatype to another datatype (as long as there is enough size for the destination)
my 2cents.
Edited by - DerekSaw on August 4, 2001 12:30:51 PM
eg.
union{ DWORD dw; struct { WORD w1; WORD w2; }};
or do a memcpy(...) from one datatype to another datatype (as long as there is enough size for the destination)

my 2cents.

Edited by - DerekSaw on August 4, 2001 12:30:51 PM
"after many years of singularity, i'm still searching on the event horizon"
My version''s about the same as AP''s, but might be a little faster:
-Deku-chan
DK Art (my site, which has little programming-related stuff on it, but you should go anyway^_^)
|
-Deku-chan
DK Art (my site, which has little programming-related stuff on it, but you should go anyway^_^)
You could always stoop down to assembly for these kind of ops:
God knows it it''s faster or not, I just like assembly
|
God knows it it''s faster or not, I just like assembly

quote:
Original post by Anonymous Poster
A word to 2 bytes:b[0]=a-((a>>8)<<8); // lowordb[1]=a>>8; // hiword
A dword to 2 words:b[0]=a-((a>>16)<<16); // lowordb[1]=a>>16; // hiword
and a dword to 4 bytes? :-)
Surely typecasting is the fastest, the computer doesn''t care, only the compiler

char c[4];// a word*((short*)&c[0]) = 12345; // or just *((short*)c) - I prefer to be explicit// a dword*((long*)&c[0]) = 123456789;// an object of class Foo*((Foo*)&c[0]) = myfoo;
See the pattern? A pointer into memory is just a pointer into memory, you can tell the compiler to interpret it as anything you want it to be.
Having said that, don''t do it if you can avoid it, type safety exists for a reason. The only violation of type safety should be the use of void*.
--
quote:
Original post by Turtlebread
b[0] = a; //I know this won''t work, but I want to make it so that b[0] has the upper end of the word, and b[1] has the lower.
I just re-read this ... is it important that b[0] has the upper and b[1] has the lower? Are you doing platform-independent data storage? The stuff that I and a few others suggested with the type-casting will do different things on different platforms
--
August 06, 2001 09:41 AM
I don''t like typecasting, I''ll do it like this:
word to 2 bytes:
highbyte=w>>8
lowbyte=w&0xFF
dword to 2 words:
highword=dw>>16
lowword=dw&0xFFFF
dword to 4 bytes:
b[0]=dw>>24
b[1]=(dw>>16)&0xFF
b[2]=(dw>>8)&0xFF
b[3]=dw&0xFF
But, ofcourse, well done typecasting is alot faster.
word to 2 bytes:
highbyte=w>>8
lowbyte=w&0xFF
dword to 2 words:
highword=dw>>16
lowword=dw&0xFFFF
dword to 4 bytes:
b[0]=dw>>24
b[1]=(dw>>16)&0xFF
b[2]=(dw>>8)&0xFF
b[3]=dw&0xFF
But, ofcourse, well done typecasting is alot faster.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement