Jump to content
  • Advertisement

Archived

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

Turtlebread

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster

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

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

Share this post


Link to post
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 this post


Link to post
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 byte

b[1]=a & 0xFF

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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
it should be a & ~0xFF

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
BYTE b[50];
WORD a;

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

Share this post


Link to post
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 this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!