• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Setting two BYTES equal to a WORD

## Recommended Posts

Just a quick question here. I''ve got an array of BYTES. I want to fill it with a bunch of WORDS. Example: WORD a; BYTE b[50]; 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. What is the best way to accomplish this? I thought about bitshifting the upper "BYTE" of the WORD to a seperate BYTE, and doing the same with the lower. I just want to know if this is the fastest way w\out dropping to ASM. Thanks, Chris

##### Share on other sites
Guest Anonymous Poster

WORD w;
BYTE b[50];
WORD *pw = (WORD) b;

for(int i = 0; i < 25; i++)
pw = w;

##### Share on other sites
Guest Anonymous Poster
Whoops it went wrong:

WORD w;
BYTE b[50];
WORD *pw = (WORD) b;

for(int i = 0; i < 50/sizeof(WORD); i++)
*pw = w;

##### Share on other sites
Ksero    262
quote:
Original post by KalvinB
WORD a
char b[50]

b[0]=a/256
b[1]=a%256

I just want to point out that it''s faster to do:
  b[0]=a >> 8 //shifts the word right 8 bits, effectively dividing by 256//0xFF is a byte filled with 1''s. Implicitly, the high byte is all 0''s//Make a bitwise AND, and you get the low byteb[1]=a & 0xFF

Then again, a good compiler might be able to do that for you...
//Ksero

##### Share on other sites
Guest Anonymous Poster
it should be a & ~0xFF

##### Share on other sites
Ksero    262
I don''t agree, AP. b[1] was supposed to hold the low byte of a, which is what my code does.
a & ~0xFF would be 0, wouldn''t it?
~0xFF = 0x00
a & 0x00 = 0

##### Share on other sites
Guest Anonymous Poster
typedef char BYTE;
WORD a;
BYTE b[50];
BYTE c;

c = a;
b[1] = c;
c = (a>>8);
b[0] = c;

##### Share on other sites
Guest Anonymous Poster
BYTE b[50];
WORD a;

for (int i = 0; i < 50; i+=2)
{
*((WORD *)&b) = a;
}

##### Share on other sites
Guest Anonymous Poster
Oh, sorry the UBB code mangled the previous post...

BYTE b[50];
WORD a;
for (int index = 0; index < 50; index+=2)
{
*((WORD *)&b[index]) = a;
}

That should do it

##### Share on other sites
Zaei    122
How would one go about sticking 2 WORDs into a single DWORD?

Z.

##### Share on other sites
Guest 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

##### Share on other sites
DerekSaw    243
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

##### Share on other sites
DekuTree64    1168
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^_^)

##### Share on other sites
Zipster    2359
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

##### Share on other sites
taliesin73    122
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!

##### Share on other sites
taliesin73    122
quote:

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!

##### Share on other sites
Guest Anonymous Poster
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.