Advertisement

Setting two BYTES equal to a WORD

Started by August 02, 2001 02:08 AM
16 comments, last by Turtlebread 23 years, 6 months ago
How would one go about sticking 2 WORDs into a single DWORD?

Z.
______________"Evil is Loud"
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 
Advertisement
If maths looks ugly to u... may be u could use union .
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:
  BYTE b[50];WORD w;for(int index = 0; index < 25; index++)   ((WORD*)b)[index] = w;  




-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:
  // ...BYTE b[NUM_WORDS*2];WORD w[NUM_WORDS]; // Assume we want to turn all these words into byte pairs :)_asm {   mov cx,NUM_WORDS   START:   mov bx,w[NUM_WORDS-cx]   mov b[2*(NUM_WORDS-cx)],bh   mov b[2*(NUM_WORDS-cx)+1],bl   loop START}  

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*.



--


Get a stripper on your desktop!

Advertisement
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


--


Get a stripper on your desktop!

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.

This topic is closed to new replies.

Advertisement