Setting two BYTES equal to a WORD

Started by
16 comments, last by Turtlebread 22 years, 8 months ago
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
Advertisement
WORD a
char b[50]

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

Ben


Icarus Independent

Jump Down The Rabbithole

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

for(int i = 0; i < 25; i++)
pw = w;
Whoops it went wrong:

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

for(int i = 0; i < 50/sizeof(WORD); i++)
*pw = w;
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
it should be a & ~0xFF
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
typedef char BYTE;
WORD a;
BYTE b[50];
BYTE c;

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

for (int i = 0; i < 50; i+=2)
{
*((WORD *)&b) = a;
}
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

This topic is closed to new replies.

Advertisement