Jump to content
elspxvwcyr

What Order Are Integer Bytes Stored?

Recommended Posts

What order are 2 and 4 byte integers stored in a PIC16?

 

Thanks...

Compile the following code and run the debugger, add the variable "var"

to the watchwindow, then you will see it's "little endian".

 

#include <system.h>



union{
     unsigned long four;
     unsigned short two[2];
     unsigned char one[4];
    }test;


unsigned char array[]={0x11,0x22,0x33,0x44};

unsigned long result;


int main(void)
{
 test.four = 0x44332211;
 result = test.four;
 result = test.two[0];
 result = test.one[0];

 result = *((unsigned long *)test.one);
 result = *((unsigned short *)test.one);
 result = *((unsigned char *)test.one);

 result = *((unsigned long *)array);
 result = *((unsigned short *)array);
 result = *((unsigned char *)array);

 while(1);
}

 

p.s.: I am not sure if this is always the case, maybe Dave or Pavel can acknowledge

this? Is it safe to use a union this way in BoostC?

 

Grtz, Teuniz

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...