#### Archived

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

# Setting two BYTES equal to a WORD

This topic is 6194 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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

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

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

##### Share on other sites
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
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
it should be a & ~0xFF

##### Share on other sites
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
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
BYTE b[50];
WORD a;

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

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

1. 1
2. 2
Rutin
22
3. 3
4. 4
JoeJ
16
5. 5

• 14
• 29
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
631775
• Total Posts
3002278
×