Setting two BYTES equal to a WORD
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
August 02, 2001 02:39 AM
WORD w;
BYTE b[50];
WORD *pw = (WORD) b;
for(int i = 0; i < 25; i++)
pw = w;
August 02, 2001 02:40 AM
Whoops it went wrong:
WORD w;
BYTE b[50];
WORD *pw = (WORD) b;
for(int i = 0; i < 50/sizeof(WORD); i++)
*pw = w;
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:
|
Then again, a good compiler might be able to do that for you...
//Ksero
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
a & ~0xFF would be 0, wouldn''t it?
~0xFF = 0x00
a & 0x00 = 0
August 03, 2001 10:06 PM
typedef char BYTE;
WORD a;
BYTE b[50];
BYTE c;
c = a;
b[1] = c;
c = (a>>8);
b[0] = c;
WORD a;
BYTE b[50];
BYTE c;
c = a;
b[1] = c;
c = (a>>8);
b[0] = c;
August 04, 2001 12:34 AM
BYTE b[50];
WORD a;
for (int i = 0; i < 50; i+=2)
{
*((WORD *)&b) = a;
}
WORD a;
for (int i = 0; i < 50; i+=2)
{
*((WORD *)&b) = a;
}
August 04, 2001 12:39 AM
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
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
Popular Topics
Advertisement
Recommended Tutorials
Advertisement